I want to write a python regex expression to match following text
< 0 | d/dR | 1>
I could do something like this
r"<\s*(\d )\s*\|\s*xx\s*\|\s*(\d )\s*>"
but can not figure out how to do the fraction part in the middle (d/dR) represented as xx. Any help? Thank you!!
CodePudding user response:
You can use
<\s*(\d )\s*\|\s*([^<>]*?)\s*\|\s*(\d )\s*>
See the regex demo.
Note the [^<>]*? is used here, it matches any zero or more chars other than < and > chars, as few as possible. < and > are negated because < and > are left- and right-hand delimiters here, and the negated character class helps prevent matching across < and getting unwelcome matched.
See the regex demo. Details:
<- a<char\s*- zero or more whitespaces(\d )- Group 1: one or more digits\s*\|\s*-|enclosed with zero or more whitespaces([^<>]*?)- Group 2: any zero or more chars other than<and>chars, as few as possible\s*\|\s*-|enclosed with zero or more whitespaces(\d )- Group 3: one or more digits\s*- zero or more whitespaces>- a>char.
