Python中的正则表达式匹配单词后和尖括号之间的文本 [英] Regex expression in Python to match text after words and between angle brackets

查看:20
本文介绍了Python中的正则表达式匹配单词后和尖括号之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这个字符串:用户IP地址分配",

如何提取用户"和地址后括号中的内容.那就是我想取上面的字符串并返回

How do I extract the things in brackets after 'User' and Address. That is I would like to take the above string and return

(IwantThis, IalsoWantThis)

推荐答案

试试这个,它会匹配<>之间的所有文本:

Try this, it'll match all the text between <>:

s = "Group <stuffhere> User <IwantThis> IP <notimportant> Address <IalsoWantThis> assigned"
ans = re.findall(r'<(.+?)>', s)

现在可以轻松提取我们感兴趣的部分:

Now it's easy to extract the parts that we're interested in:

ans[1]
=> 'IwantThis'
ans[3]
=> 'IalsoWantThis'

这篇关于Python中的正则表达式匹配单词后和尖括号之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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