python:多行正则表达式 [英] python: multiline regular expression

查看:64
本文介绍了python:多行正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

试试这个:

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

这篇关于python:多行正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆