开发者

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

I'm looking to extract,

ID=(?P<group>.+?);

from a string, the 'ID=' is a consta开发者_如何学JAVAnt, 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 ';' ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜