2 回答
TA贡献1818条经验 获得超8个赞
您正在查找数字之间的一个或多个非空格( '\S+'
),后跟句点,后跟空格 ( '\d+\.\s'
),以及空格后跟短划线 ( '\s-'
):
pattern = r'\d+\.\s(\S+)\s-' [re.findall(pattern, l)[0] for l in your_list]
TA贡献1770条经验 获得超3个赞
你的正则表达式模式:
pattern = r"""
\d+ # 1 or more digits
\. # Escaped period character
\s+? # 1 or more whitespace
(\w+) # 1 or more alphabetic characters
\s+ # 1 or more whitespace
- # hyphen
.* # zero or more of anything besides newline.
"""
字符串列表:
words = [ "1. hello - jeff", "2. gello - meff", "3. fellow - gef", "12. willow - left"]
for word in words:
# capture results in a variable
# re.X for verbose pattern format.
tmp = re.search(pattern, word, flags = re.X)
# If variable is not None, print results of the first captured group.
if tmp:
print(tmp.group(1))
输出:
hello
gello
fellow
willow
添加回答
举报