正则表达式匹配数字组合,但不应匹配某些其他模式

时间:2015-02-09 09:23:45

标签: python regex

我试图匹配以下内容。

10/1987 - 10/1987
2/1987 - 12/1987
More clearly:

 ["one" or "two digit"]["any symbol mainly [/-]][a space if there followed by slash or hiphen [-/]["one" or "two digit"]["any symbol mainly [/-]]

这是我的输出。

https://regex101.com/r/cN9eJ0/1

1 个答案:

答案 0 :(得分:1)

\b((?:(?<!\/)[\d]{1,2}[\/\s-]{0,3}(?!\d{3}\b)\d{2,4})\s*-\s*\b(?:(?<!\/)[\d]{1,2}[\/\s-]{0,3}(?!\d{3}\b)\d{2,4}))

只需添加锚点即可限制部分匹配。请参阅演示。

https://regex101.com/r/cN9eJ0/5