开发者

python re: no such group

I'm newbie in Python. I can't understand why this code does not work:

reOptions = re.search(
    "[\s+@twitter\s+(?P<login>\w+):(?P<password>.*?)\s+]",
    document_text)
if reOptions:
    login = reOptions.group('login')
    password = reOptions.group('password')

I'm开发者_StackOverflow having an error:

IndexError: no such group

With document_text

Blah-blah
[ @twitter va1en0k:somepass ]


You need to escape the brackets [ and ] as \[ and \].

\[\s+@twitter\s+(?P<login>\w+):(?P<password>.*?)\s+\]


The [ and ] are special regular expression characters. Escape them to match literal [ and ]. See Regular Expression Syntax.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜