VSCode regex find & replace submatch math? - regex

%s#{fileID: \(213[0-9]*\)#\='{fileID: '.(submatch(1)-1900)#
I am using this regex search and replace command in vim to subtract a constant from each matching id.
I can do the regex find in VSCode but how can I reference the submatch for maths & replace? submatch(1) does not work in VSCode?
Thanks.

Given a regular expression of (foobar) you can reference the first group using $1 and so on if you have more groups in the replace input field.

To augment Benjamin's answer with an example:
Find Carrots(With)Dip(Are)Yummy
Replace Bananas$1Mustard$2Gross
Result BananasWithMustardAreGross
Anything in the parentheses can be a regular expression.

Just to add another example:
I was replacing src attr in img html tags, but i needed to replace only the src and keep any text between the img declaration and src attribute.
I used the find+replace tool (ctrl+h) as in the image:

For beginners, the accepted answer is correct, but a little terse if you're not that familiar with either VSC or Regex.
So, in case this is your first contact with either:
To find and modify text,
In the "Find" step, you can use regex with "capturing groups," e.g. I want to find (group1) and (group2), using parentheses. This would find the same text as I want to find group1 and group2, but with the difference that you can then reference group1 and group2 in the next step:
In the "Replace" step, you can refer to the capturing groups via $1, $2 etc, so you could change the sentence to I found $1 and $2 having a picnic, which would output I found group1 and group2 having a picnic.
Notes:
Instead of just a string, anything inside or outside the () can be a regular expression.
$0 refers to the whole match

In my case $1 was not working, but $0 works fine for my purpose.
In this case I was trying to replace strings with the correct format to translate them in Laravel, I hope this could be useful to someone else because it took me a while to sort it out!
Search: (?<=<div>).*?(?=</div>)
Replace: {{ __('$0') }}
Regex Replace String for Laravel Translation

Another simple example:
Search: style="(.+?)"
Replace: css={css`$1`}
Useful for converting HTML to JSX with emotion/css!

Just another example for someone figuring out.
In this example, I've added #### to the start of the string and placed the first group $1 after that. Everything outside group (.*) is going to be deleted.
<h4.*">(.*)</h4>
#### $1
# before:
<h4 id="extract-inline-json-with-regex">Extract inline JSON data with Regex</h4>
# after:
#### Extract inline JSON data with Regex

Related

VSCode - find and replace with regexp, but keep word

I have multiple occurance of src={icons.ICON_NAME_HERE} in my code, that I would like to change to name="ICON_NAME_HERE".
Is it possible to do it with regular expressions, so I can keep whatever is in code as ICON_NAME_HERE?
To clarify:
I have for example src={icons.upload} and src={icons.download}, I want to do replace all with one regexp, so those gets converted to name="upload" and name="download"
Try searching on the following pattern:
src=\{icons\.([^}]+)\}
And then replace with your replacement:
name="$1"
In case you are wondering, the quantity in parentheses in the search pattern is captured during the regex search. Then, we can access that captured group using $1 in the replacement. In this case, the captured group should just be the name of the icon.

Notepad++ replace text with RegEx search result

I would like replace a standard string in a file, with another that is a result of a regular expression. The standard text looks like:
<xsl:variable name="ServiceCode" select="###"/>
I would like to replace ### with a servicecode, that I can find later in the same file, from this URL:
<a href="/Services/xyz" target="_self">
The regular expression (?<=\/Services\/)(.*)(?=\" )
returns the required service code "xyz".
So, I opened Notepad++, added "###" to the "Find what" and this RegEx to the "Replace with" section, and expected that the ### text will be replaced by xyz.
But I got this result:
<xsl:variable name="ServiceCode" select="?<=/Services/.*?=" "/>
I am new to RegEx, do I need to use different syntax in the replace section than I use to find a string? Can someone give me a hint how to achieve the required result? The goal is to standardize tons of files with similar structure as now all servicecodes are hardcoded in several places in the file. Thanks.
You could use a lookahead for capturing the part ahead.
Search for: (?s)###(?=.*/Services/([^"]+)") and replace with: $1
(?s) makes the dot also match newlines (there is also a checkbox available in np++)
[^"] matches a character that is not "
The replacement $1 corresponds to capture of first parenthesized subpattern.
I am no expert at RegEx but I think I may be able to help. It looks like you might be going at this the wrong way. The regex search that you are using would normally work like this:
The parenthesis () in RegEx allow you to select part of your search and use that in the replace section.
You place (?<=\/Services\/)(.*)(?=\" ) into the "Find what" section in Notepad++.
Then in the "Replace with" section you could use \1 or \2 or \3 to replace the contents of your search with what was found in the (?<=\/Services\/) or (.*) or (?=\" ) searches respectively.
Depending on the structure of your files, you would need to use a RegEx search that selects both lines of code (and the specific parts you need), then use a combination of \1\2\3 etc. to replace everything exactly how it was, except for the ### which you could replace with the \number associated with xyz.
See http://docs.notepad-plus-plus.org/index.php/Regular_Expressions for more info.

Using parameters in regular expressions

I am trying to use NotePad++ to do a search and replace using the regex function that replaces a string of characters but maintains one part of the string. My description isn't very good so perhaps it will be better if I just give you the example.
Throughout and xml doc I have the following elements...
<AddressLine3>addressLine3>
<AddressLine2>addressLine2>
I want to replace these with
<addressLine3> <addressLine2>
So I need to maintain the address line number.
I know that
AddressLine([0-9]{1})>addressLine([0-9]{1})
is a valid reg ex but I'm not sure what to put in the replace with section to tell it to maintain whatever value was found by ([0-9]{1}).
Thanks.
It's \{number of the group}, so \1, \2, ...
Edit with your precisions (I changed a bit your regex for simpler groups):
(AddressLine[0-9]{1}>)(addressLine[0-9]{1}) is replaced by \2
You can capture it in group and replace them
Find:(AddressLine[0-9])>(addressLine[0-9])
Replace:$1 <$2
Find what : (<AddressLine\d>)AddressLine\d
Replace by: $1
You have to select the choice regular expression

find and replace regex for reformatting the value

I have a text like :value_subvalue that I want to transform to :value[:subvalue].
I have a find & replace field in Coda where I can enter a regex to find and replace.
Any idea how to do this?
You haven't said what language, so this is a generic answer:
Search regex: (:[a-z]+)_([a-z]+)
Replacement: \1[:\2]
If you're doing this in java, the group refs would be $1 and $2

How do I access matched objects for replacement when using regular expression mode in PL/SQL Developer Find & Replace?

I have a query where I want to replace
avg(j2)
with
avg(case when j2 <> 0 then j2 else 0 end)
The above is a specific example but the pattern is the same with all the replacements. It's always a word followed by a number that needs to be replaced with the case statement that checks if the number is not 0.
I tried the following for find:
avg(\(\w\d\))
and the find works. Now, I want to do a replace so I try:
avg(case when \1 <> 0 then \1 else 0 end)
but it puts literal \1 and not the captured text from the match. I tried \\1 & $1 as well and it takes all of them literally. Can anyone tell me what the right syntax is for using the captured text for replacement? Is this supported?
Thanks,
Ashish
I am not sure if the PL/SQL Developer IDE supports group capture. The recent versions do seem to support regex based find and replace though. Cant find a source to confirm if group capture works.
Why dont you try pasting the code in a something like Notepad++ and try the same regex. It should work. You could paste the result back to your IDE and continue from there...
You can replace it using $ and number like,
$0 or $1 etc. see an example below
find: TABLE (.*\..*) IS
replace: COLUMN $1 IS
http://regexr.com/3gm6c