我的字符串包含text = "a) Baghdad, Iraq b) United Arab Emirates (possibly)"
我想把它分成["Baghdad, Iraq","United Arab Emirates (possibly)"]
我使用的代码没有给我提供所需的结果
re.split('\\s*([a-zA-Z\\d][).]|•)\\s*(?=[A-Z])', text)
关于这件事请帮帮我
最佳答案
您可以使用list comp和第二个regex为示例创建所需的数据:
import re
text = "a) Baghdad, Iraq b) United Arab Emirates (possibly)"
# different 1.regex pattern, same result - refining with 2nd pattern
data = [x for x in re.split(r'((?:^\s*[a-zA-Z0-9]\))|(?:\s+[a-zA-Z0-9]\)))\s*',
text) if x and not re.match(r"\s*[a-zA-Z]\)",x)]
print(data)
输出:
['Baghdad, Iraq', 'United Arab Emirates (possibly)']
见https://regex101.com/r/wxEEQW/1