Using regex to make edits on multiple lines - regex

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.

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

Extracting String using regex

I am using a HTA Application I wrote for our help desk to take notes.
I've been using regex (Best I can) to CTRL+A our ticket pop up and click parse on my app to fill out information
I need to find "TICKET - T00000000.0000 - Account Security (Company Name...)" and only grab the "Account Security" section. or for future grab whatever is between the 2nd - and the (
Any suggestions would be grand
here is an example what I've tried and what I am using
try {
$(".problem_description", context).val(clipdata.match(/TICKET -.+[)]/)[0]);
}
catch (e) {
}
Update
I have tried a few of the suggestions here but the results still seem to give me the entire string or error out in my script.
Here's the regex using positive lookbehind:
(?<=TICKET\ -\ T\d{8}\.\d{4}\ -\ ).*\)
Here's regex101 explanation: https://regex101.com/r/6BN16e/1
The query effectively says matching anything after "TICKET - T(8 digits).(4 digits) - ". You can of course tweak it to your specification.
Here's a tutorial on lookahead and lookbehind that may be helpful: https://www.regular-expressions.info/lookaround.html
Use a capture group. In a regex you can use parentheses to mark a capture group. So if you define a pattern where a portion of it marks the text you want to extract, you can wrap that portion in parentheses. The object returned by the match function in most languages is an object that lets you access the values of individual capture groups.
Try this regex I quickly made up: /[^-]*-[^-]*- ([^(]*)/
Full example: var matches = "TICKET - T00000000.0000 - Account Security (Company Name...)".match(/[^-]*-[^-]*- ([^(]*)/)
Your value will be in matches[1].
It says: start from the beginning, look for anything not a dash, then a dash, then anything not a dash, then another dash, then a space, then capture anything not a left-parenthesis into a capture group.
This one will leave an extra space at the end of the captured group value. Also, it will truncate your value if your value contains a left parenthesis.

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.

Changing some XML tags names but leaving unchanged values between them

In one of my XML file I need to find and replace some opening tags names using regex and Notepad++. Also I need to leave unchanged every text between them.
Example:
<uri>http://domain-name.com/41874_01_home_big.jpg</image_big>
I need to change into:
<image_big>http://domain-name.com/41874_01_home_big.jpg</image_big>
For some reasons I can't just change uri tag, cause there are others closing tags like /image_small in the document (opened with uri of course).
I tried to change it like:
<uri>.*?</image_big>
But I don't know with what I should replace it.
I tried with:
<image_big>\1</image_big>
but result is:
<image_big></image_big>
without any text inside.
I need your help. I'm not good with regex.
Just put .*? inside a capturing group.
<uri>(.*?)<\/image_big>
Then replace the match with <image_big>\1</image_big> or <image_big>$1</image_big>
Your regex <uri>.*?</image_big> matches correctly but in-order to fetch all the characters which are matched by .*? pattern, you must need to put that pattern inside a capturing group. So that we could back-reference it for later use.
DEMO
Find:<uri>(.*?)</image_big>
Replace:<image_big>\1</image_big> or <image_big>$1</image_big>
See demo.
https://www.regex101.com/r/rK5lU1/19

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.