I'm looking to extract,
ID=(?P<group>.+?);
from a string, the 'ID=' is a constant, group can be anything. The match's position will vary in the string.
This is fine in most cases, however, occasionally the match will be at the end of the string and the semi-colon will be missing. In this case, how do I optionally match the end of the string? I tried the following:
ID=(?P<group>.+?)[;$]
But this didn't seem to work, I imagine because $ is not a character (it's an anchor?).
This is being done in Python using the re module, and all normal behaviour such as using raw strings has been accounted for (I think!).
You can use (;|$)
to match it. Or if you don't want a capture, (?:;|$)
if the option re.MUTILINE is NOT enabled ($ means end of string)
ID=(?P<group>)[^;]+
if the option re.MUTILINE IS enabled ($ means end of line)
ID=(?P<group>)[^;\r\n]+
What do you capture, by the way : what is between (?P<group>) and ';' ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With