VS Code regex replace - regex

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.

Related

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')

Go regex, Negative Look Ahead alternative

I am trying to implement the regex (?<!\\{)\\[[a-zA-Z0-9_]+\\](?!\\}) with go regex.
Match value will be like [ua] and [ua_enc] and unmatched should be {[ua]} and {[ua_enc]}
As Negative lookahead is not supported in Go, what may be the alternative expression for this?
There is no alternative expression for this. Using plain (?:[^{]|^)(...)(?:[^}]|$) to capture the intended match and assert the previous and next characters are not braces will kind-of work: you will need to work with the first capture group instead of with the full match, and it will fail when there is only a single character between two matches (e.g. [foo]_[bar]). The best way, really, is to use FindAllStringSubmatchIndex and manually check the previous and next characters to make sure they are not braces outside of regexp.

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.

How to recall the match side on the substitution side of your replace command?

For example, to transform foo(a,b,c) into foo(ax,bx,cx) with a single command you would need something like:
:s/[abc]/MATCHED_CHARACTERx/g
What is the correct syntax for this substitution?
With escaped parentheses \(...\) in the search pattern you can mark sections that you want to use in the replace pattern. Each section in parentheses can be accessed by \1, \2, \3.... \0 matches the whole search pattern.
So, for your example, you could either use
:%s/[abc]/\0x/g
or, to be more specific,
:%s/foo(\(.\),\(.\),\(.\))/foo(\1x,\2x,\3x)/g
The second pattern will match only foo(<any>,<any>,<any>) instead of all occurrences of a,b,c.
The & is a synonym of \0 and stands for the entire match. So you can also use &x in your example replacement. This also works in many other tools, e.g. sed.

How to search (using regex) for a regex literal in text?

I just stumbled on a case where I had to remove quotes surrounding a specific regex pattern in a file, and the immediate conclusion I came to was to use vim's search and replace util and just escape each special character in the original and replacement patterns.
This worked (after a little tinkering), but it left me wondering if there is a better way to do these sorts of things.
The original regex (quoted): '/^\//' to be replaced with /^\//
And the search/replace pattern I used:
s/'\/\^\\\/\/'/\/\^\\\/\//g
Thanks!
You can use almost any character as the regex delimiter. This will save you from having to escape forward slashes. You can also use groups to extract the regex and avoid re-typing it. For example, try this:
:s#'\(\\^\\//\)'#\1#
I do not know if this will work for your case, because the example you listed and the regex you gave do not match up. (The regex you listed will match '/^\//', not '\^\//'. Mine will match the latter. Adjust as necessary.)
Could you avoid using regex entirely by using a nice simple string search and replace?
Please check whether this works for you - define the line number before this substitute-expression or place the cursor onto it:
:s:'\(.*\)':\1:
I used vim 7.1 for this. Of course, you can visually mark an area before (onto which this expression shall be executed (use "v" or "V" and move the cursor accordingly)).