Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的数据模式如下所示,
t_hat_1 t_hat_2 t_hat_3 name s_flower_1 s_flower_2 s_flower_3 s_flower_22 s_flower_23 address
我需要识别所有重复匹配项,例如 t_hat_1,2,3 和 s_flower_1,2,3,22,23...我没有固定数量的重复项..例如:-hat 将有 [1- 3]和花[1-50]
在 Python 正则表达式中识别这些元素的最有效方法是什么。
re.findall将返回所有匹配项:
re.findall
import re data = """ t_hat_1 t_hat_2 t_hat_3 name s_flower_1 s_flower_2 s_flower_3 s_flower_22 s_flower_23 address """ flowers_patterns = re.findall('(s_flower_\d+)', data) hat_patterns = re.findall('(t_hat_\d+)', data)