Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions Matching on multiple separated characters

Tags:

regex

In this string:

"<0> <<1>> <2>> <3> <4>"

I want to match all instances of "<\d{1,2}>" except those I have escaped with an extra set of triangle brackets, e.g., I want to match 0,2,3,4 but not 1, e.g.:

"<0> <<1>> <2>> <3> <4>"

I want to do this in one single regular expression but the best I could get is:

(^|[^\<])\<(?<1>\d{1,2})>([^>]|$)

Which will match 0,3,4 but not 2, e.g.:

"<0> <<1>> <2>> <3> <4>"

Does anyone know how this can be done with a single regular expression?

like image 720
nferr Avatar asked Mar 01 '23 17:03

nferr


1 Answers

You can also try conditionals: (?(?<=<)(<\d{1,2}>(?!>))|(<\d{1,2}>))

like image 144
Bojan Resnik Avatar answered Mar 17 '23 01:03

Bojan Resnik