独立数字正则表达式? [英] Standalone numbers Regex?
本文介绍了独立数字正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用此正则表达式:
I currently use this regex:
(\d+)
我可以得到2个字符串的问题:
the problem that i can get 2 strings:
"2112343和4.99"
"2112343 and alot of 4.99"
OR
"4.99和很多2112343"
"4.99 and alot of 2112343 "
我从两个地方都得到了这个
I get this from both:
[2112343, 4, 99]
我只需要获取 2112343 ...我该如何实现?
I need to get only the 2112343... How can i achieve this?
推荐答案
使用环视,您可以将捕获范围限制为仅由其他数字或小数点包围的数字:
Using lookaround, you can restrict your capturing to only digits which are not surrounded by other digits or decimal points:
(?<![0-9.])(\d+)(?![0-9.])
或者,如果您只想匹配独立编号(例如,如果您不想匹配 abc123def
中的123):
Alternatively, if you want to only match stand-alone numbers (e.g. if you don't want to match the 123 in abc123def
):
(?<!\S)\d+(?!\S)
这篇关于独立数字正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文