Regular Expression Notepad++ replace - regex

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

Related

Regex match but do not capture with a ".*"

I have two blocks of code.
<TabItem Header="{materialDesign:PackIcon Kind=Bank, Size=24}"
Style="{StaticResource MaterialDesignNavigationRailTabItem}"
ToolTip="Example">
and
<Button Header="{materialDesign:PackIcon Kind=Bank, Size=24}"
Style="{StaticResource MaterialDesignNavigationRailTabItem}"
ToolTip="Example">
I to select the ToolTip="Example" part and replace it. However, I only want to select the ToolTip that is inside the TabItem block. I can select it with:
ToolTip\=\"(.*?)\"
That selects the one in the Button block as well. I used this StackOverflow Question to try and solve my issue, but I could not figure out how to make it work with a ".*".
So my criteria is:
Must begin with '<TabItem'
Must contain 'ToolTip=".*"'
Must end with ">"
Is what I am trying to achieve possible? If so, how could I achieve this?
This should work
<TabItem\b[^>]*\bToolTip="([^"]*)"[^>]*>
Using [^>]* ensures that the regexp won't match across multiple tags, and [^"]* won't allow that capture group to go outside the quoted tooltip attribute.
You can't use a lookaround for this, because you'd need a lookbehind to match the part before ToolTip, and lookbehinds have to be fixed length in most regexp engines.
If you're using this in a regexp replacement, put the parts that should be kept in the replacement into capture groups, and then use back-references in the replacement string.
<TabItem.*?ToolTip="(.*?)">
if you use this regex then you will be able to get the value inside the tooltip as a list you can use them in regex with $1 here $1 will be the value inside the first tooltip and $2 will have the second if there is a second match for this regex

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.

How to replace all strings to numbers contained in each string in Notepad++?

I'm trying to find all values with following pattern :
value="4"
value="403"
value="200"
value="201"
value="116"
value="15"
and replace it with value inside scopes.
I'm using the following regex to find the pattern :
.*"\d+"
How can I do a replacement?
In Notepad++ to replace, hit Ctrl+H to open the Replace menu.
Then if you check the "Regular expression" button and you want in your replacement to use a part of your matching pattern, you must use "capture groups" (read more on google). For example, let's say that you want to match each of the following lines
value="4"
value="403"
value="200"
value="201"
value="116"
value="15"
using the .*"\d+" pattern and want to keep only the number. You can then use a capture group in your matching pattern, using parentheses ( and ), like that: .*"(\d+)". So now in your replacement you can simply write $1, where $1 references to the value of the 1st capturing group and will return the number for each successful match. If you had two capture groups, for example (.*)="(\d+)", $1 will return the string value and $2 will return the number.
So by using:
Find: .*"(\d+)"
Replace: $1
It will return you
4
403
200
201
116
15
Please note that there many alternate and better ways of matching the aforementioned pattern. For example the pattern value="([0-9]+)" would be better, since it is more specific and you will be sure that it will match only these lines. It's even possible of making the replacement without the use of capture groups, but this is a slightly more advanced topic, so I'll leave it for now :)
psxls gave a great answer but I think my Notepad++ version is slightly different so the $ (dollar sign) capturing did not work.
I have Notepad++ v.5.9.3 and here's how you can accomplish your task:
Search for the pattern: value=\"([0-9]*)\"
And replace with: \1 (whatever you want to do around that capturing group)
Ex. Surround with square brackets
[\1] --> will produce value="[4]"
Replace (.*")\d+(")
With $1x$2
Where x is your "value inside scopes".
I have Notepad++ v6.8.8
Find: [([a-zA-Z])]
Replace: [\'\1\']
Will produce:
$array[XYZ] => $array['XYZ']
Find: value="([\d]+|[\d])"
Replace: \1
It will really return you
4
403
200
201
116
15
js:
a='value="4"\nvalue="403"\nvalue="200"\nvalue="201"\nvalue="116"\nvalue="15"';
a = a.replace(/value="([\d]+|[\d])"/g, '$1');
console.log(a);

Notepad++ regular expression for XML replace

I need to replace following lines in XML file:
hashName="'Miecz Nieb. Wojownika+5IMiecz Nieb. Wojownika+5" name="Miecz Nieb. Wojownika+5"
As the above line is not correct, I want it to be replaced like this:
hashName="'Miecz Nieb. Wojownika+5'" name="Miecz Nieb. Wojownika+5"
(It should take the item name from the name="" attr!).
This is what I got at the moment, its not working as expected since it does remove my name="..." attribute.
Search for:
hashName="(')(.*)"(.)name="(.*)"(.)/
Replace with:
hashName="'\4'" name="\4"
For this simple example this is working
Search for
hashName="[^"]*"\s*name="([^"]*)"
and replace with
hashName="'\1'" name="\1"
If you don't want to capture or group characters, don't put brackets around it, therefor I removed most of them.
To avoid that too much is matched, e.g. if you have two "name" attributes in one row, I used [^"]* to do a non greedy matching.
This should work
Search for: hashName=\".+\" name=\"(.+)\"
Replace with: hashName="'\1'" name="\1"