...">

Python re: no such group

I am new to Python. I can't figure out why this code doesn't 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 got the error:

IndexError: no such group

With document_text

Blah-blah
[@twitter va1en0k: somepass]
+2


source to share


2 answers


[

and ]

are special regex characters. Run them to match the literal [

and ]

. See Regular Expression Syntax .



+3


source


You need to avoid the brackets [and] both \[

and \]

.



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

      

+4


source







All Articles