python:多行正则表达式

fac*_*cha 2 python regex

我有一段文字,我必须解析用户名和哈希值.现在我正在使用两个正则表达式.我可以只使用一个多行正则表达式吗?

#!/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)
Run Code Online (Sandbox Code Playgroud)

bal*_*loo 5

试试这个:

re.findall(r'Hello, (?P<login>[^.]+)\..+?hash: (?P<hash>[^.]+)', test_str, re.S)
Run Code Online (Sandbox Code Playgroud)