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
非捕获组(?:
匹配10-49[1-4]\d
或|
匹配5050
关闭组)
匹配A-M[A-M]
result = re.findall(r"/STAR20(?:[1-4]\d|50)[A-M]", text)
也许可以?然后可以使用set
获取不同的值。