Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for optionally matching the end of a string (optional $)

Tags:

python

regex

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!).

like image 787
mrmagooey Avatar asked Feb 07 '11 06:02

mrmagooey


2 Answers

You can use (;|$) to match it. Or if you don't want a capture, (?:;|$)

like image 129
Lily Ballard Avatar answered Oct 06 '22 01:10

Lily Ballard


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 ';' ?

like image 41
eyquem Avatar answered Oct 06 '22 02:10

eyquem