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.
精彩评论