VSCode - find and replace with regexp, but keep word - regex

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.

Related

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.

How to Use Delphi TRegEx to replace a particular capture group?

I am trying to use TRegex in Delphi XE7 to do a search and replace in a string.
The string looks like this "#FXXX(b, v," and I want to replace the second integer value v.
For example:
#F037(594,2027,-99,-99,0,0,0,0)
might become
#F037(594,Fred,-99,-99,0,0,0,0)
I am a newbie at RegEx but made up this pattern that seems to work fine for finding the match and identifying the right capturing group for the "2027" (the part below in parentheses). Here it is:
#F\d{3}(\s*\d{1,5}\s*,\s*(\d{1,5})\s*,
My problem is that I cannot work out how to replace just the captured group "2027" using the Delphi TRegEx implementation. I am getting rather confused about TMatch and TGroup and how to use them. Can anyone suggest some sample code? I also suspect I am not understanding the concept of backreferences.
Here is what I have so far:
Uses
RegularExpressions;
//The function that does the actual replacement
function TForm6.DoReplace(const Match: TMatch): string;
begin
//This causes the whole match to be replaced.
//#F037(594,2027,-99,-99,0,0,0,0) becomes Fred-99,-99,0,0,0,0)
//How to just replace the first matched group (ie 2027)?
If Match.Success then
Result := 'Fred';
end;
//Code to set off the regex replacement based on source text in Edit1 and put the result back into Memo1
//Edit1.text set to #F037(594,2027,-99,-99,0,0,0,0)
procedure TForm6.Button1Click(Sender: TObject);
var
regex: TRegEx;
Pattern: string;
Evaluator: TMatchEvaluator;
begin
Memo1.Clear;
Pattern := '#F\d{3}\(\s*\d{1,5}\s*,\s*(\d{1,5})\s*,';
RegEx.Create(Pattern);
Evaluator := DoReplace;
Memo1.Lines.Add(RegEx.Replace(Edit1.Text, Pattern, Evaluator));
end;
When using regex replacements, the whole matched content will be replaced. You have access to the whole match, captured groups and named captured groups.
There are two different ways of doing this in Delphi.
You are currently using an Evaluator, that is a object method containing instructions what to replace. Inside this method you have access to the whole match content. The result will be the replacement string.
This way is useful if vanilla regex is not capable of things you want to do in the replace (e.g. increasing numbers, changing charcase)
There is another overload Replace method that uses a string as replacement. As you want to do a basic regex replace here, I would recommend using it.
In this string you can backreference to your matched pattern ($0 for whole match, $Number for captured groups, ${Name} for named capturing groups), but also add whatever characters you want.
So you can capture everything you want to keep in groups and then backreference is as recommended in Wiktors comment.
As you are doing a single replace, I would als recommend using the class function TRegex.Replace instead of creating the Regex and then replacing.
Memo1.Lines.Add(
TRegex.Replace(
Edit1.Text,
'(#F\d{3}\(\s*\d{1,5}\s*,\s*)\d{1,5}(\s*,)',
'$1Fred$2'));
PCRE regex also supports \K (omits everything matched before) and lookaheads, which can be used to capture exactly what you want to replace, like
Memo1.Lines.Add(
TRegex.Replace(
Edit1.Text,
'#F\d{3}\(\s*\d{1,5}\s*,\s*\K\d{1,5}(?=\s*,)',
'Fred'));

Regular Expression Notepad++ replace

I'm trying to replace only the 'remove' strings in this block of code:
<table asldkjf>
remove
remove
remove
<tr>
The problem is keeping the 'asldkjf' string within the table tag.
I've tried replacing
<table[^>]*>[^<]*<tr>
with
<table[^>]*><tr>
And I got
<table[^>]*><tr>
Which is incorrect as it replaced the 'asldkjf' with '[^>]*'
I've tried replacing based on reference
<table[^>]*>[^<]*<tr>
with
<table$1><tr>
And I got
<table><tr>
Which is also incorrect.
Use capturing group and surround the pattern, whose text you want to use in the replacement. You can refer to them with $n, where n is the number of the capturing group.
For more information about how capturing group works, look at this answer.
Find what:
(<table[^>]*>)[^<]*(<tr>)
Replace with:
$1$2
You can use this reegx
(?<=<table asldkjf>).*(?=<tr>)
and replace with empty string. Use dotall modifier.
DEMO

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