用于获取产品ID的正则表达式

问题描述 投票:0回答:1

我在让这个正则表达式仅匹配以 1 开头的项目产品 ID 时遇到问题。到目前为止,这是我提出的模式。

def find_productID(report):
  pattern = r'\d{4}-[A-Z][A-Z]-\d{2}' #enter the regex pattern here
  result = re.findall(pattern, report) #enter the re method  here
  return result



print(find_productID("Products 1234-AB-30 and 2234-AB-30, not items 12-AB-30 or 12345-AB-30")) # Should return ['1234-AB-30']
print(find_productID("Products of interest are 1234-AB-30, 1678-XZ-11, and 1561-CD-57. We're not interested in other products like 2345-AB-29.")) # Should return ['1234-AB-30', '1678-XZ-11', '1561-CD-57']

r'\d{4}-[A-Z][A-Z]-\d{2}'。

python regex
1个回答
0
投票

使用此模式将第一个数字视为 1:

r'1\d{3}-[A-Z][A-Z]-\d{2}'

© www.soinside.com 2019 - 2024. All rights reserved.