Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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 使用正则表达式获取唯一id_Python_Regex - Fatal编程技术网

Python 使用正则表达式获取唯一id

Python 使用正则表达式获取唯一id,python,regex,Python,Regex,我有以下案文: This is the foo test the date purchase id is /STAR2015A. This is another foo test the purchase is /STAR2022M. Yet another foo test, get it back by if u dont like, purchase id is /STAR2039K. You wont be surprised if i write another id /STAR205

我有以下案文:

This is the foo test the date purchase id is /STAR2015A. This is another foo test the purchase is /STAR2022M. Yet another foo test, get it back by if u dont like, purchase id is /STAR2039K. You wont be surprised if i write another id /STAR2050L. 
我想获得所有唯一的购买ID。它每次都以/STAR开头,以字母A-M结尾。此外,数字范围从2010年到2050年。我尝试了以下操作,但没有返回任何结果:

import re
dset = []

text = "This is the foo test the date purchase id is /STAR2015A. This is another foo test the purchase is /STAR2022M. Yet another foo test, get it back by if u dont like, purchase id is /STAR2039K. You wont be surprised if i write another id /STAR2050L. "

pattern = re.findall("[^\/STAR[20][10-50][A-M]]",text)

print(pattern)
请告诉我如何解决此问题。

您可以使用

/STAR20(?:[1-4]\d|50)[A-M]
  • /STAR20
    逐字匹配
  • (?:
    非捕获组
    • [1-4]\d
      匹配10-49
    • |
    • 50
      匹配50
  • 关闭组
  • [A-M]
    匹配A-M
|

范例

result = re.findall(r"/STAR20(?:[1-4]\d|50)[A-M]", text)
也许可以?然后可以使用
set
获取不同的值。