text1="worda wordb aaaa wordc wordd worde 10, 20 and 35."
text2="wordf bbbbb wordg wordh 11, 20 and 35."
text3="wordi 11, 33, 44."
要求是:当且仅当行中出现了 aaaa 这个字符串, 返回所有数字.
我尝试了以下方法:
import re
re.findall(r'(?:aaaa).*(\d+)', text1) # ['5']
re.findall(r'(?:aaaa).*?(\d+)', text1) # ['10']
# 期待结果是["10", "20", "35"]
# 虽然可以这样,但是我希望能有个一行的正则,而不是匹配 2 次
if re.findall("aaaa", text1):
numbers = re.findall("(\d+)", text1)
先行谢过啦~
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.