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

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

Related

RegEx for transforming the next text using PhpStorm's search and replace dialog

I need to transform text using regex
TPI +2573<br>
NM$ +719<br>
Молоко +801<br>
Прод. жизнь +6.5<br>
Оплод-сть +3.6<br>
Л. отела 6.3/3.9<br>
Вымя +1.48<br>
Ноги +1.61<br>
to this one
<strong>TPI</strong> +2573<br>
<strong>NM$</strong> +719<br>
<strong>Молоко</strong> +801<br>
<strong>Прод. жизнь</strong> +6.5<br>
<strong>Оплод-сть</strong> +3.6<br>
<strong>Л. отела</strong> 6.3/3.9<br>
<strong>Вымя</strong> +1.48<br>
<strong>Ноги</strong> +1.61<br>
Is it possible with regex in PhpStorm's search and replace dialog?
Given your text, you can use this regex,
.* +
and replace it with <strong>$0</strong> (Notice there is a space after </strong>)
We're using .* to capture everything but stop just before one (possible one or more) space because that's the point after which we want the text to remain intact. Once we capture the text, we use back-reference $0 to replace the match with <strong>$0</strong> so only matched text is placed within <strong> tags.
Regex Demo
Just in case, if this doesn't work for any of the samples you haven't included in your post, then please list the rules of replacement and I will give you a more robust solution, that will work flawlessly for your given rules.

Using regex to make edits on multiple lines

I have downloaded an sql file and would like to mass replace some names of pages I have created.
This is an example of one page:
{\"ref\":\"Session_1___Pg1___V2\",\"pageTitle\":null,\"description\":null,\"revealDate\":0,\"gQRef\":null,\"lQRef\":null,\"gQScore\":null,\"lQScore\":null,\"newsfeedDates\":null,\"subtitle\":null,\"pageLinkTitle\":null,\"linkTitle\":null,\"pageBack\":null,\"pagePrint\":false,\"visitedFlag\":null,\"widthPercentage\":0,\"maxWidth\":0,\"thumbnail\":null,\"edit\":null,\"copy\":null,\"delete\":null,\"preview\":null}]}
How do I search and highlight all the references of each page, just like Session_1___Pg1___V2 from above, without selecting anything else. I have hundreds of pages that I need to change the references of and I think regex would be the best way to do it with.
I used (\"((.*?))\") but it would select everything that is inbetween quotes. How do I just select the ref of the pages?
Use a lookbehind:
(?<=\\"ref\\":\\)"([^"]+)"
(?<=\\"ref\\":\\) Lookbehind for \"ref\":\ substring.
"([^"]+)" Matches ", opens capture group, capturing anything other than a ". Then closes capturing group and matches ".
The result is group:
Group 1. 11-32 `Session_1___Pg1___V2\`
Regex demo here.

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.

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

RegExp , Notepad++ Replace / remove several values

I have this dataset: (about 10k times)
<Id>HOW2SING</Id>
<PopularityRank>1</PopularityRank>
<Title><![CDATA[Superior Singing Method - Online Singing Course]]></Title>
<Description><![CDATA[High Quality Vocal Improvement Product With High Conversions. Online Singing Lessons Course Converts Like Crazy Using Content Packed Sales Video. You Make 75% On Every Sale Including Front End, Recurring, And 1-click Upsells!]]></Description>
<HasRecurringProducts>true</HasRecurringProducts>
<Gravity>45.9395</Gravity>
<PercentPerSale>74.0</PercentPerSale>
<PercentPerRebill>20.0</PercentPerRebill>
<AverageEarningsPerSale>74.9006</AverageEarningsPerSale>
<InitialEarningsPerSale>70.1943</InitialEarningsPerSale>
<TotalRebillAmt>16.1971</TotalRebillAmt>
<Referred>75.0</Referred>
<Commission>75</Commission>
<ActivateDate>2011-06-23</ActivateDate>
</Site>
I am trying to do the following:
Get the data from within the tags, and use it to create a URL, so in this example it should make
http://www.reviews.how2sing.domain.com
also, all other data has to go, i want to perform a REGEX function that will just give me a list of URLS.
I prefer to do it using notepad++ but i suck at regex, any help would be welome
To keep the regex relatively simple you can just use:
.*?<id>(.+?)</id>
Replace with:
http://www.reviews.\1.domain.com\n
That will search and replace all instances of Id tag and preceding text. You can then just remove the last manually.
Make sure matches newline is selected.
Regex is straightforward, only slightly tricky part is that it uses +? and *? which are non-greedy. This prevents the whole file from being matched. The () indicate a capture group that is used in the replacement, i.e. \1.
If you want to a regex that will include replacing the last part then use:
.*?(?:(<id>)?(.+?)</id>).+?(?:<id>|\Z)
This is a bit more tricky, it uses:
?:. A non-capturing group.
| OR
\Z end of file
Basically, the first time it will match everything up to the end of the first </id> and replace up to and including the next <id>. After that it will have replaced the starting <id> so everything before </id> goes in the group. On the last match it will match the end of file \Z.
If you only want the Id values, you can do:
'<Id>([^<]*)<\/Id>'
Then you can get the first captured group \1 which is the Id text value and then create a link from it.
Here is a demo:
http://regex101.com/r/jE9qN8
[UPDATE]
To get rid of all other lines, match this regex: '.*<Id>([^<]*)<\/Id>.*' and replace by first captured group \1. Note for the regex match, since there are multiple lines, you will need to have the DOTALL or /s flag activated to also match newlines.
Hope that helps.