正则表达式匹配字符串前两位数字的模式

Regex to match pattern of two digit number before a string

我有这样的文字:

text = "There is 18 years of experience in marketing, with 5 years in sales."

我想创建一个将提取 \d\d + "years" 的正则表达式规则,以便它可以提取任何一位和两位数字,后跟关键字 'years' 或 'Years' 或 'Yrs' 来自任何语料库。

预期输出:[“18 年”,“5 年”]

我试过了

text = re.findall(r'\d\d +years', text)

返回 None

如何获取?

试试这个模式:\d{1,2} (?:yrs|years?) with flag IGNORECASE in python.

参见正则表达式 Demo

说明

  • \d{1,2}:捕获一个或two-digit.
  • (?:: 是一个non-captured组。
  • yrs|years:这会捕获“yrs”或“years”。 (case-insensitive)
  • ?:"y"后面的这个表示"s"可以是也可以不是。