Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for nested values

I want a regex that can parse ignoring the nested matches

I mean on this for example:

/*asdasdasd /* asdasdsa */ qweqweqwe */

to match the first "/*" with the last "*/" and not stopping to the first "*/"

Thanks...

like image 660
Kostas Avatar asked Mar 04 '26 21:03

Kostas


2 Answers

RegEx expressions will naturally be greedy, so you can just use:

\/\*.*\*\/

If you wanted it to do what you're afraid of and make the RegEx be lazy and stop after the first match you'd have to add an ? like:

\/\*.*?\*\/
like image 189
mVChr Avatar answered Mar 06 '26 18:03

mVChr


Regular expressions can't count nested items by definition (though implementations do go further than the computer scientific definition).

See http://en.wikipedia.org/wiki/Regular_expression#Expressive_power_and_compactness

like image 22
StackExchange saddens dancek Avatar answered Mar 06 '26 18:03

StackExchange saddens dancek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!