查找模式前面没有数字的正则表达式匹配
我目前正在尝试从字符串中提取日期。以下是一些示例:
02.10 abcdef -> extract '02.10'
abcdef 03.12 -> extract '03.12'
abcdef 308.56 -> extract nothing
一个简单的正则表达式,例如 (\d{2}.\d{2})
对于前两种情况工作正常,但我在第三个示例中发现了误报,即正则表达式返回 08.56
,这是有道理的。
有什么办法可以防止这个字符串被提取吗?我尝试了 [^0-9](\d{2}.\d{2})
,它似乎在正则表达式调试网站上工作,但当我将其编译为 python 正则表达式时却不起作用,
import re
regex = re.compile(r'[^0-9](\d{2}.\d{2})')
谢谢进步
I am currently trying to extract dates from strings. Here are a few examples:
02.10 abcdef -> extract '02.10'
abcdef 03.12 -> extract '03.12'
abcdef 308.56 -> extract nothing
A simple regex such as (\d{2}.\d{2})
works fine for the first two cases but I catch a false positive for the third example, the regex returns 08.56
, which makes sense.
Is there any way to prevent this string from being extracted? I tried [^0-9](\d{2}.\d{2})
which seems to be working on regex debugging websites but not when I compile it as a python regex with
import re
regex = re.compile(r'[^0-9](\d{2}.\d{2})')
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我首先认为您需要完整的数字,可以通过以下方式实现:
(\d*\.\d{2})
返回 308.56但随后看到了这一行:
那么答案应该是:
您可以在 https://regex101.com/ 上测试它
I First thought you needed the complete number which is possible with:
(\d*\.\d{2})
which returns 308.56But then is saw the line:
Then the answer should be:
You can test it on https://regex101.com/