python: multiline regular expression
Posted
by facha
on Stack Overflow
See other posts from Stack Overflow
or by facha
Published on 2010-05-25T09:08:26Z
Indexed on
2010/05/25
9:21 UTC
Read the original article
Hit count: 237
Hi, everyone
I have a piece of text and I've got to parse usernames and hashes out of it. Right now I'm doing it with two regular expressions. Could I do it with just one multiline regular expression?
#!/usr/bin/env python
import re
test_str = """
Hello, UserName.
Please read this looooooooooooooooong text. hash
Now, write down this hash: fdaf9399jef9qw0j.
Then keep reading this loooooooooong text.
Hello, UserName2.
Please read this looooooooooooooooong text. hash
Now, write down this hash: gtwnhton340gjr2g.
Then keep reading this loooooooooong text.
"""
logins = re.findall('Hello, (?P<login>.+).',test_str)
hashes = re.findall('hash: (?P<hash>.+).',test_str)
© Stack Overflow or respective owner