Regex - How to match on all text inside of ${} - regex

I need a regular expression to match all text inside of ${}.
i.e:
For the following case:
Hello, {does this work ${yes} I hope} so and this is another ${case}
It should match on "yes" and "case".

Use the following pattern:
\$\{(.*?)\}
Or, if your regex engine/flavor does not support capture groups but does support lookarounds, you may use:
(?<=\$\{).*?(?=\})

Related

Regular Expression to return number without coma

I have to extract a number formatted xx,xxx.xx in a different format - xxxxx.xx by applying a regular expression. In other words, I have to remove the comma from the number in the final capture group.
I am not quite sure if it's possible to achieve only with the regular expression and without writing specific code to split and join at these values.
Here is the demo.
This is the part of input string:
AMT : EGP 3,000.00
My current regex is AMT\s*:\s*EGP\s*(\d*,\d*.\d*), which basically retreives 3,000.00.
I'm expecting to have 3000.00 in final capture group.
EDIT:
Since the OP doesn't want to capture and replace, the following can be done:
AMT\s*:\s*EGP\s*(\d*),(\d*.\d*)
The expected data is now part of the two capturing groups, and can be accessed by concatenating them: \1\2.
Demo
You can capture everything other than the , in two groups, and then replace:
Capture with:
(AMT\s*:\s*EGP\s*\d*),(\d*.\d*)
Replace with: \1\2
Demo
Try this:
AMT\s*:\s*EGP\s*\K\d+(,\d{3})*(\.\d+)?
Here is Demo
After find the match, do something like: Mystring.Replac(",", "")

Notepad++ Regex to find group of lines with condition

Given this example text:
<abr:rules>
<abr:ruleTypeDefinition>
<abr:code>ABB</abr:code>
<abr:ownership>
<abr:owner organization="NT" application="DCS" subapplication="FM"/>
...lines...
...........
</abr:rules>
<abr:rules>
<abr:ruleTypeDefinition>
<abr:code>ADE</abr:code>
<abr:ownership>
<abr:owner organization="NT" application="DCS" subapplication="CM"/>
...lines...
...........
</abr:rules> (end of group)
I would like to find and remove all that goes from <abr:rules> to </abr:rules> with the condition that subapplication IS NOT "CM". Organization and application are the same, <abr:code> it's any string.
What I tried so far is
<abr:rules>\n<abr:ruleTypeDefinition>\n<abr:code>[a-zA-Z0-9]{3,}<\/abr:code>\n<abr:ownership>\n<.*"(FM|PSD|SSC)"\/>\n(?s).*?\n<\/abr:rules>\n
which works but only because I know the other subapplication names.
Is there any way to do it with Regex only ?
Try the following find and replace:
Find:
<abr:rules>((?!subapplication=).)*subapplication="(?!CM")[^"]+"((?!</abr:rules>).)*</abr:rules>
Replace:
(empty string)
Demo
Note: The above pattern will only work if you enable dot in Notepad++ to match newlines. If you don't want to do that, then you may use [\S\s] instead of dot.
You should not use regex for xml, you can read why here:
https://stackoverflow.com/a/1732454/3763374
Instead you can use some parser like Xpath

Regular Expressions (pcre) for shortcode/bbcode

I have a regex (see on https://regex101.com/r/mB7vQ8/2):
/\[content_box((.*?)!?\])(.*?)\[\/content_box\]/ig
for match all [content_box] (with or without tag parameters) in a text like:
[content_boxes foo=bar][content_box baz=foo]text[/content_box][/content_boxes]
[content_box]text[/content_box]
[content_box foo=bar]text[/content_box]
My regex work, but if [content_box] is included in a [content_boxes] the rule fails the match (in strong):
[content_boxes foo=bar][content_box baz=foo]text[/content_box][/content_boxes]
[content_box]text[/content_box]
[content_box foo=bar]text[/content_box]
the expected match is:
[content_boxes foo=bar][content_box baz=foo]text[/content_box][/content_boxes]
[content_box]text[/content_box]
[content_box foo=bar]text[/content_box]
see online https://regex101.com/r/mB7vQ8/2
How solve it?
You can use this regex with word boundaries:
~\[content_box\b\s*([^]]*)\](.*?)\[/content_box\]~
RegEx Demo
Here content_box\b will not match content_boxes and match will always be inner [content_box ..] tag.

Regex expression to extract strings

I have the following expression:
MN=Abc123,MN=sssa,MN=abc adsa 1,MN=&3ams d'amé,MN=dat,CB=ds,CB=ds
How can I extract one by one the expressions following MN= ?
eg: firstly I want to extract Abc123, secondly I wnat sssa and so on ...
Appreciate your answer!
Use a capture group:
"[A-Z]{2}=([^,]+)"
Then get the first group form your matched object.
Or if the language you are dealing for supports the look-around you can use a positive look-behind in order to directly match the expected parts:
"(?<=[A-Z]{2}=)[^,]+"
If your regex environment supports lookbehind, you can extract desired information with this regex:
Environment supporting Lookbehind
(?<=MN=)(.*?)(?=,)
Environment not supporting Lookbehind
(?:MN=)(.*?)(?=,)
Your desired result will be stored in Group 1, aka $1
Based on your input string, here is result
Abc123
sssa
abc adsa 1
&3ams d'amé
dat
See live demo here

Notepad++ and delimiters: automatically replace ``string'' by \command{string}

Within Notepad++, I want to replace many instances of the type ``string'' by \command{string} where string can be any string of characters. I am fairly close to what I want to achieve with:
Find: (?<=``)(.*?)(?='')
Replace: \\command{\1}
There is still a problem. With the regex code above, instead of \command{string} I get ``\command{string}'' and I am not sure why the `` and '' are not removed?
It is because you are using lookaround assertions. Lookaround (zero-width) assertions only assert that a position can be matched and do not "consume" any characters on the string. You can use the below regular expression.
Find: ``([^']+)''
Replace: \\command{\1}
You need to wrap everything into a capture group and use that. NP++ seems to not support lookahead/behind, but you dont need that for this specific case anyway:
``([^']+)'' -> \\command{\1}
This will make sure it does not match two commands (longest match) in something like:
run ``ls -l'' or ``ls -a''