在python中,在字符串中匹配标记的多个实例中的所有内容

在python中,在字符串中匹配标记的多个实例中的所有内容,python,regex,string,Python,Regex,String,示例字符串: str = "<sec>John</sec> said hi to a woman (named <sec>Mary</sec>)" 我现在应该已经真正学会了正则表达式。试试这个: import re str = "<sec>John</sec> said hi to a woman (named <sec>Mary</sec>)" ext = re.findall(r'<sec

示例字符串:

str = "<sec>John</sec> said hi to a woman (named <sec>Mary</sec>)"
我现在应该已经真正学会了正则表达式。

试试这个:

import re
str = "<sec>John</sec> said hi to a woman (named <sec>Mary</sec>)"
ext = re.findall(r'<sec>(\S+?)</sec>', str)
重新导入
str=“约翰向一位名叫玛丽的女士问好”
ext=re.findall(r'(\S+?)',str)
这将返回
['John','Mary']

\S
-表示匹配任何非空白字符

+?
-表示将字符重复一次或多次(非贪婪)

()
-表示提取这些括号内的所有内容。

您正在处理(类似)XML。使用

将xml.etree.ElementTree作为ET导入
str=“约翰向一位名叫玛丽的女士问好”
doc=ET.fromstring(“+str+”)
结果=[doc.findall(“.//秒”)中x的x.text]
#>>>['John','Mary']

或者你真的应该学会谷歌;)二重奏!:)谢谢-1无需尝试,请注意使用
re
解析
xml
格式非常容易出错。您应该使用诸如
xml
lxml
之类的库。OP不太可能只搜索4个字母的字符串。啊,没看到这个!这似乎也工作得很好!谢谢它不仅有效,而且更能抵抗失败,更灵活。我认识到这一点!我已经改变了被接受的答案。我对你的问题投了赞成票,以抵消反对票,但下次请你表现出自己的努力。这通常在StackOverflow周围受到好评。
import re
str = "<sec>John</sec> said hi to a woman (named <sec>Mary</sec>)"
ext = re.findall(r'<sec>(\S+?)</sec>', str)
import xml.etree.ElementTree as ET

str = "<sec>John</sec> said hi to a woman (named <sec>Mary</sec>)"

doc = ET.fromstring("<root>" + str + "</root>")
result = [x.text for x in doc.findall(".//sec")]

# >>> ['John', 'Mary']