Using parameters in regular expressions - regex

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

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.

Replace string using regular expression in KETTLE

I would like to use regular expression for replacing a certain pattern in the Kettle. For example, AAAA >5< BBBB, I want to replace this with AAAA 555 BBBB. I know how to find the pattern, but I am not sure how to replace that with new string. The one thing I have to keep is that I have to find pattern together ><, not separately like > or < because there is another pattern <5>.
You can use the "Replace in String" step in a transformation.
Set use RegEx to "Y", type your regex on the Search box, with capturing groups if necessary, and the replacement string in the replacement box, referring to capture groups as $1, $2, ...
It'll replace all occurrences of the regex in the original string.
If the Out Stream field is ommitted, it'll overwrite the In stream field.
If you want the pattern >\d< replaced by a triple of the found digit, you can use Replace-In-String in regex mode:
Search: (.*)(>(\d)<)(.*)
Replace: $1$3$3$3$4
If you want all such patterns treated the same:
Search: (>(\d)<)
Replace: $2$2$2
EDIT due to your improved requirement
Since you intend to convert your "simple" markup to a more HTML-like markup, you better use a User-Defined-Java-Expression. Also, you must avoid to reintroduce simple markup when replacing repeatedly.

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.

regex expression for selecting a value

I want to write a regexp formula for the below sip message that takes number:
< sip:callpark#as1sip1.com:5060;user=callpark;service=callpark;preason=park;paction=park;ptoken=150009;pautortrv=180;nt_server_host=47.168.105.100:5060 >
(Actually there are "<" and ">" signs in the message, but the site does not let me write)
For this case, I want to select ptoken value.. I wrote an expression such as: ptoken=(.*);p but it returns me ptoken=150009;p, I just need the number:150009
How do I write a regexp for this case?
PS: I write this for XML script..
Thanks,
I SOLVE THE PROBLEM BY USING TWO REGEX:
ereg assign_to="token" check_it="true" header="Refer-To:" regexp="(ptoken=([\d]*))" search_in="hdr"/
ereg assign_to="callParkToken" search_in="var" variable="token" check_it="true" regexp="([\d].*)" /
You could use the following regex:
ptoken=(\d+)
# searches for ptoken= literally
# captures every digit found in the first group
Your wanted numbers are in the first group then. Take a look at this demo on regex101.com. Depending on your actual needs, there could be better approaches (Xpath? as tagged as XML) though.
You should use lookahead and lookbehind:
(?<=ptoken=)(.+?)(?=;)
It captures any character (.+?) before which is ptoken= and behind which is ;
The <ereg ... > action has the assign_to parameter. In your case assign_to="token". In fact, the parameter can receive several variable names. The first is assigned the whole string matching the regular expression, and the following are assigned the "capture groups" of the regular expression.
If your regexp is ptoken=([\d]*), the whole match includes ptoken which is bad. The first capture group is ([\d]*) which is the required value. Thus, use <ereg regexp="ptoken=([\d]*)" assign_to="dummyvar,token" ..other parameters here.. >.
Is it working?

VSCode regex find & replace submatch math?

%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