使用 re.search 在 Python 中查找所有文本
Find in all text using the re.search in Python
感谢您的光临。
这是我的代码。
import re
content = '가나다라,456456 show, 가나다라<>'
a = re.search("[^a-zA-Z<>]+", content)
print(a.group())
Output : 가나다라,456456.
但我想要这个
Output : 가나다라,456456 , 가나다라
也就是说,我要搜索全文。
我能做什么? :(
您需要使用 re.findall
顺序来获得所有匹配项。 re.search
必须 return 只有第一个匹配项。
re.findall(r"[^a-zA-Z<>]+", content)
示例:
>>> import re
>>> content = '가나다라,456456 show, 가나다라<>'
>>> ''.join(re.findall("[^a-zA-Z<>]+", content))
'가나다라,456456 , 가나다라'
感谢您的光临。
这是我的代码。
import re
content = '가나다라,456456 show, 가나다라<>'
a = re.search("[^a-zA-Z<>]+", content)
print(a.group())
Output : 가나다라,456456.
但我想要这个
Output : 가나다라,456456 , 가나다라
也就是说,我要搜索全文。 我能做什么? :(
您需要使用 re.findall
顺序来获得所有匹配项。 re.search
必须 return 只有第一个匹配项。
re.findall(r"[^a-zA-Z<>]+", content)
示例:
>>> import re
>>> content = '가나다라,456456 show, 가나다라<>'
>>> ''.join(re.findall("[^a-zA-Z<>]+", content))
'가나다라,456456 , 가나다라'