Python-查找所有正则表达式
我有一个文本文件中的假ID列表。我想捕获所有以“A0015”开头的ID。我尝试了不同的正则表达式,但它们没有捕获最终输出。应该使用哪个正则表达式Python-查找所有正则表达式,python,python-3.x,Python,Python 3.x,我有一个文本文件中的假ID列表。我想捕获所有以“A0015”开头的ID。我尝试了不同的正则表达式,但它们没有捕获最终输出。应该使用哪个正则表达式 text = "Here are the fake student ids: IDs A0015-4737, IDs: A0015-384721-ADA2ad, A0015WE382 \n A00152838. Please enter this." capture_id_list = (re.findall(r"A0015 ([\w-]+)", te
text = "Here are the fake student ids: IDs A0015-4737, IDs: A0015-384721-ADA2ad, A0015WE382 \n A00152838. Please enter this."
capture_id_list = (re.findall(r"A0015 ([\w-]+)", text,flags=re.IGNORECASE))
print(capture_id_list) # results with []
# print(text.startswith('A0015')) # Gives False...not usefull
find_this = "A0015"
capture_id_list = text[:text.find(find_this) + len(find_this)]
print(capture_id_list) # Here are the fake student ids: IDs A0015. Not the results
最终输出:
['A0015-4737','A0015-384721-ADA2ad','A0015WE382','A00152838']
我建议在代码中使用r“(A0015[^,.]+)”
:
>>>import re
>>>text = "Here are the fake student ids: IDs A0015-4737, IDs: A0015-384721-ADA2ad, A0015WE382 \n A00152838. Please enter this."
>>>capture_id_list = (re.findall(r"(A0015[^ ,.]+)", text,flags=re.IGNORECASE))
>>>print(capture_id_list)
['A0015-4737', 'A0015-384721-ADA2ad', 'A0015WE382', 'A00152838']
这里()是一个捕获组。它捕获以A0015开头的字符串和一个或多个不同于空格、逗号或点的字符(逗号或点)。这应该适用于您:
r“(A0015[^\s,.]*)”
,内联它看起来如下:
capture_id_list = (re.findall(r"(A0015[^\s,.]*)", text,flags=re.IGNORECASE))
(A0015[^\s,.]*)
- 第一个捕获组
(A0015[^\s,.]*)
按字面意思匹配字符A0015
(不区分大小写)A0015
- 匹配下表中不存在的单个字符:
[^\s,.]*
量词-在零和无限之间匹配,尽可能多地匹配,根据需要返回(贪婪)*
匹配任何空白字符(等于\s
)[\r\n\t\f\v]
匹配列表中的单个字符,.
(不区分大小写),.