Python中的正则表达式匹配单词后和尖括号之间的文本
问题描述:
给定这个字符串:组
,
如何提取用户"和地址后括号中的内容.那就是我想取上面的字符串并返回
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'