Regex for text before a comma within double brackets - regex

I am trying to extract text that is not only inside of double brackets but also before a comma. I have only been able to solve the two issues separately (I think) but can't figure out how to bring it together
Double brackets: """\[\[(.+?)\]\]*"""
Before comma: """([^,]+)"""

I think you definitely need to escape the []:
\[\[(.+?)\]\]*
If it needs to be before a comma as well, can't you use:
(\[\[(.+?)]]*,)
I might be missing something here. Sorry.

Related

VS Code regex replace

I want to do a global find of all instances of
TestBed.get(*)
and replace with
TestBed.inject<*>(*)
I can't seem to figure out the regex I need to just match what is between the brackets
I have tried TestBed.get(.*) and replace with TestBed.inject<$1>($1) but the ends up with an extra set of brackets
You have to escape the literal ( and )
TestBed.get\(([^)]*)\)
In TestBed.get(.*) the brackets are considered for making the groups. So you need to escape those. Try finding this instead:
TestBed.get(\(.*)\)
Also instead of a wild card pattern, you may want to use more precise patterns to match the contents inside the brackets.

Regex to allow A-D, plus symbol, ' , and brackets

I am trying to create a regex that takes in only A-D, plus symbol, ' , and brackets.
For example (A+B+C+D)(A+B)(A')
They must be all inside brackets for it to work but currently my regex allows terms outside the brackets to work too.
^[A-D\(\)\'+]+$
Need some help thanks
This should work fine:
^(?:\([A-D]'?(?:\+[A-D]'?)*\))+$
Visit this link to try out a working demo.
In order to ensure everything is inside a pair of parenthesis (()), the parenthesis should not be inside the character class. Move them outside, and surround that with a repeating non-capturing group like this:
^(?:\([A-D'+]+\))+$
This will still allow A-D, plus signs, and single quotes to appear in any order inside parenthesis. If you don't want that, the regex will need to be changed to something like this:
^(?:\([A-D]+'?(?:\+[A-D]+'?)*\))+$
This will match the following:
(A)
(A')
(A+A)
(A'+A)
(A'+A')

How to add quotations around numbers in Vim - Regex?

I have been trying, unsuccessfully, to add quotation marks around some numbers in my file using regex. To clarify, let me give an example of what I am trying to do.
Something like myFunction(100) would be changed to myFunction("100").
I thought :100,300s/\([0-9]*\)/"\0" would work but it put quotation marks around spaces as well.
What can I do to fix this?
You should slightly modify the regular expression:
%s/\(\d\+\)/"\1"
In regular expression, first matched group is \1, not \0. And it looks safer to use \+ instead of *.
The reason this isn't working as expected is because [0-9]* is matching all strings of zero length, so your substitution is adding two quotes between every two characters. Changing it to [0-9]+ (to require at least one digit) will solve your problem.
As an additional improvement, you can replace [0-9] with \d. Also, \0 is the replacement for the entire matched expression, so your parentheses are unnecessary: :100,300s/\d+/"\0" will accomplish what you want. Captured subgroups start at \1.

Regex Match between brackets (...)

I'm trying to grab 2 items from a simple line.
[Title](Description)
EDIT: actually a url looking to display called it description because i want it displayed not actually parsed.
[Trivium](https://www.youtube.com/user/trivium)
Grabbing between the brackets (...) doesn't seem to work at all for me. I've googled and found several variations with no luck, Thanks in advance :)
EDIT:
Tried the following:
[(.+?)]\((.*)\)
[(.+?)]\([^\(\r\n]*\)
[(.+?)]((.+?))
and a cpl more I cant find again
The first regex you listed almost has it right. Try using this regex instead:
\[.+?\]\((.*)\)
As #PM 77-1 pointed out, you need to escape the brackets by placing a backslash in front of them. The reason for this is that brackets are special regex metacharacters, or characters which have a special meaning. Brackets tell the regex engine to look for classes of characters contained inside of it.
Your original regex [(.+?)]\((.*)\) is actually doing this:
[(.+?)] match a period '.' 1 or more times
\((.*)\) match (anything), i.e. anything contained in parentheses
So this regex would match .....(stuff) but would not match [Title](Description), the latter which is what you really want.
Here is a link where you can test out the working regex:
Regex 101

Notepad++ masschange using regular expressions

I have issues to perform a mass change in a huge logfile.
Except the filesize which is causing issues to Notepad++ I have a problem to use more than 10 parameters for replacement, up to 9 its working fine.
I need to change numerical values in a file where these values are located within quotation marks and with leading and ending comma: ."123,456,789,012.999",
I used this exp to find and replace the format to:
,123456789012.999, (so that there are no quotation marks and no comma within the num.value)
The exp used to find is:
([,])(["])([0-9]+)([,])([0-9]+)([,])([0-9]+)([,])([0-9]+)([\.])([0-9]+)(["])([,])
and the exp to replace is:
\1\3\5\7\9\10\11\13
The problem is parameters \11 \13 are not working (the chars eg .999 as in the example will not appear in the changed values).
So now the question is - is there any limit for parameters?
It seems for me as its not working above 10. For shorter num.values where I need to use only up to 9 parameters the string for serach and replacement works fine, for the example above the search works but not the replacement, the end of the changed value gets corrupted.
Also, it came to my mind that instead of using Notepad++ I could maybe change the logfile on the unix server directly, howerver I had issues to build the correct perl syntax. Anyone who could help with that maybe?
After having a little play myself, it looks like back-references \11-\99 are invalid in notepad++ (which is not that surprising, since this is commonly omitted from regex languages.) However, there are several things you can do to improve that regular expression, in order to make this work.
Firstly, you should consider using less groups, or alternatively non-capture groups. Did you really need to store 13 variables in that regex, in order to do the replacement? Clearly not, since you're not even using half of them!
To put it simply, you could just remove some brackets from the regex:
[,]["]([0-9]+)[,]([0-9]+)[,]([0-9]+)[,]([0-9]+)[.]([0-9]+)["][,]
And replace with:
,\1\2\3\4.\5,
...But that's not all! Why are you using square brackets to say "match anything inside", if there's only one thing inside?? We can get rid of these, too:
,"([0-9]+),([0-9]+),([0-9]+),([0-9]+)\.([0-9]+)",
(Note I added a "\" before the ".", so that it matches a literal "." rather than "anything".)
Also, although this isn't a big deal, you can use "\d" instead of "[0-9]".
This makes your final, optimised regex:
,"(\d+),(\d+),(\d+),(\d+)\.(\d+)",
And replace with:
,\1\2\3\4.\5,
Not sure if the regex groups has limitations, but you could use lookarounds to save 2 groups, you could also merge some groups in your example. But first, let's get ride of some useless character classes
(\.)(")([0-9]+)(,)([0-9]+)(,)([0-9]+)(,)([0-9]+)(\.)([0-9]+)(")(,)
We could merge those groups:
(\.)(")([0-9]+)(,)([0-9]+)(,)([0-9]+)(,)([0-9]+)(\.)([0-9]+)(")(,)
^^^^^^^^^^^^^^^^^^^^
We get:
(\.)(")([0-9]+)(,)([0-9]+)(,)([0-9]+)(,)([0-9]+\.[0-9]+)(")(,)
Let's add lookarounds:
(?<=\.)(")([0-9]+)(,)([0-9]+)(,)([0-9]+)(,)([0-9]+\.[0-9]+)(")(?=,)
The replacement would be \2\4\6\8.
If you have a fixed length of digits at all times, its fairly simple to do what you have done. Even though your expression is poorly written, it does the job. If this is the case, look at Tom Lords answer.
I played around with it a little bit myself, and I would probably use two expressions - makes it much easier. If you have to do it in one, this would work, but be pretty unsafe:
(?:"|(\d+),)|(\.\d+)"(?=,) replace by \1\2
Live demo: http://regex101.com/r/zL3fY5