开发者

python re: no such group

开发者 https://www.devze.com 2022-12-09 09:16 出处:网络
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+]\",

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

精彩评论

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