Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-查找所有正则表达式_Python_Python 3.x - Fatal编程技术网

Python-查找所有正则表达式

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

我有一个文本文件中的假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-]+)", 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]
      • ,.
        匹配列表中的单个字符
        ,.
        (不区分大小写)

您的正则表达式中有一个模式所没有的空间。您可以随时玩弄您的正则表达式。