regex substitute two patterns in one match - regex

I'm trying to do a find/replace in notepad++ where the string is similar to
<span class="CharOverride-1">Q</span>
With a single replace command I'd like the result to be
<span class="somethingNew">somethingElse</span>
This matches the two things I want replaced but I don't know how to form the substitution
(?<=<span class="(CharOverride-1)">)(Q)(?=<\/span>)
If possible I'd like to avoid doing something like this
(<span class=")(CharOverride-1)(">)(Q)(<\/span>)
and
\1somethingNew\3somethingElse\5

You can simlpy use 3 captures groups:
Search:
(<span class=").*?(">).*?(</span>)
Replace:
\1somethingNew\2somethingElse\3
Don't forget to check the "regular expression" checkbox.
But, if I can give you a very personal advice: don't use Notepad++...

The regular expression (?<=<span class=")CharOverride-1">Q(?=<\/span>) uses lookahead and lookbehind to find the string CharOverride-1">Q, but only where it follows the string <span class=" and is followed by </span>. Use somethingNew">somethingElse as the replacement string.

Related

Extract multiple variable values from a single regular expression

I want to extract ID and Name from a single regular expression, but I'm not able to get the correct response
<a href="/profiles/6635/Name"
I have used below regular expression
<a href="/profiles/(.*?)/(.*?)"
As #WiktorStribiżew suggested, you should fix your regular expression to
<a href="/profiles/([^/]+)/([^/]+)"
But also use $1$ and $2$ to get both values in in Template field, for example
$1$$2$
Will save to variable concatenated value - 6635Name
What you use <a href="/profiles/(.*?)/(.*?)" is fine to capture ID and name from <a href="/profiles/6635/Name" because a lazy way (non-greedy) (.*?) you use will match only between profiles/ and the second / same like using [^\/]+ and then between / and " so , check again that you put everything right .
You may need to escape / like this \/so , change it to :
<a href="\/profiles\/(.*?)\/(.*?)"
This is your same regex here DEMO
And if you need to make sure with java tester use this tool :Java regex tester

regex required for fetching value in </span>

I need a regex for fetching the value in the </span> tag
<span class="booking-id-value">U166097</span>
value required: U166097
can please someone suggest me. I have tried using
<span class="booking-id-value">(.+?)
but it is not deriving the desired result it display on "U"
I think you need to be more specific about your expected value - below I'll just accept alphabetic and numeric characters as value - and more flexible about your tag, then I can suggest you to use a regex like this:
/<\s*span.+?class\s*=\s*"\s*booking-id-value\s*".*?>/s*([A-Za-z0-9]+)\s*<\//
Regex Demo
? after the .+ makes it ungreedy, tells it to match as little as possible - and that’s just the first U in this case.
Remove the ?, and instead look for the closing </span> after (.*) to terminate what is matched correctly:
<span class="booking-id-value">(.+)<\/span>
https://regex101.com/r/vt4pgN/1/
Regex:
<span.*>(.*)<\/span>
Substitute with:
$1
Result

Sublime text replace pattern with regex

Using Sublime Text 3 I am trying to find all instances of a <span> element where the class value is not enclosed in quotes – e.g. <span class=foo> – and I want to wrap the class value in quotes.
The following is not working as expected as a search + replace with the regex option activated:
Find what: <span class=[A-Za-z0-9]*>
Replace with: <span class="$1">
The result I am getting (which I don't want) is <span class="">
Highlighting shows that the search term is correctly matching what I want but the $1 part where I want to insert the previously captured pattern does not work. I have also tried \1 in the replace pattern.
What is wrong with my syntax?
The answer was supplied as comment. The pattern to be captured was not wrapped in brackets.
Tell it what you want to (capture): <span class=([A-Za-z0-9]*)>
Alex K.

Sublime: replace everything between quotes

I need some help with Regular expression to Search and Replace in Sublime to do the following.
I have HTML-code with links like
href="http://www.example.com/test=123"
href="http://www.example.com/test=6546"
href="http://www.example.com/test=3214"
I want to replace them with empty links:
href=""
href=""
href=""
Please help me to create a Reg. ex. filter to match my case. I guess it would sound like "starts with Quote, following with http:// .... ends with Quote and has digitals and '=' sign", but I'm not very confident of how to write this in Reg. ex. way.
(?<=href=")[^"]*
Try this.Replace by empty string.
See demo.
https://regex101.com/r/sH8aR8/40

Regular expressions: Find and replace url with c:url tag

I have a problem to build good regular expressions to find and replace. I need to replace all urls in many .jsf files. I want replace ulrs staring by XXX with <c:url value="URL_WITHOUT_XXX"/>. Examples below.
I stuck with find regular expression "XXX(.*)" and replace expression "<c:url value="\1"/>", but my find expression match to long string , for example "XXX/a" style="", but need that match only to first " (href end). Anybody helps ?
I have:
<a href="XXX/a" style="">
<a href="XXX/b" >
<a href="XXX/c" ...>
I want:
<a href="<c:url value="/a"/>" style="">
<a href="<c:url value="/b"/>" >
<a href="<c:url value="/c"/>" ...>
PS: Sorry for my poor english ;)
Edit:
I use Find/Replace in Eclipse (regular expressions on)
You should specify the language you're working with.
The following regex will match what you want:
<a href="XXX[^\"]*"
If you want to have some particular value, you can group the regex according to your needs. For example:
<a href="(XXX[^\"]*)"
will give you in the first group:
XXX/a
XXX/b
XXX/b
If you want to have only /a, /b, and /c, you can group it like that:
<a href="XXX([^\"]*)"
Edit:
I will explain what <a href="XXX[^\"]*" does:
It will match: <a href="XXX
Then it should match anything that except a " zero or many times: [^\"]*
Finally match the ", which is not really necessary
When you do: [^abc] you're telling it to match anything but not a, or b, or c.
So [^\"] is: Match anything except a ".
And the quantifier * means zero or more times, so a* will match either an empty string, or a, aa, aaa, ...
And the last thing: Groups
When you want to keep the value appart from the entire match, so you can do anything with it, you can use groups: (something).