正则表达式-仅查找整数-问题 [英] Regex - to find whole numbers only - problem

查看:74
本文介绍了正则表达式-仅查找整数-问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道上面有很多类似的线程,我已经尝试了解决方案,但是尽管阅读了它们,但似乎无法解决我的问题-请原谅我.

I am aware that there are quite a few similar threads on this and I've tried the solutions but I can't seem to solve my problems despite reading them - please pardon me.

  • Regex - Find numbers that do not contain two decimal places
  • Regex for whole numbers only and not a blank string
  • REGEX for whole, positive number
  • What's the regular expression for positive whole numbers only? (zero is not allowed)
  • Regex that accepts only numbers (0-9) and NO characters
  • Regex to match only numbers in currency

谁能告诉我下面的这两个代码有什么问题,以致于不能满足我仅选择整数的标准. (请参阅下面的两张图片)

Could any one tell me what's wrong with these two codes below such that it isn't able to fulfil my criteria of selecting only whole numbers. (refer to the two images below)

选项1 -如何阻止选择2000.5美元,因为它不是整数?

Option 1 - how to I prevent $2000.5 from being selected since it isn't a whole number?

\$\d+[^.|x](?![0-9])

选项2 -此代码未选择$ 2000.5,但未选择$ 100(这是一个整数).

Option 2 - This code doesn't select $2000.5 but it isn't selecting $100 (which is a whole number).

\$\d+$

数据

5x Item C $2.5x5, $2.50x1
1 Book $2.55
1x Table $2.59
2x Item A $2000.5x2, 10x Item B $0.5x10
1x Test $2.513, 5x Pear $100x5
$1.430
$1.50 
$1.55
10x Tee $0.5x10
50x SSS $6.5x50
$2.5, $50

非常感谢您.

推荐答案

我建议使用以下模式:

\$\d+(?![0-9.])

这与$匹配,后跟一系列的一个或多个数字.末尾(?![0-9.])的负前瞻确保我们不匹配具有小数部分的数字.

This matches $, followed by a series of one or more digits. The negative lookahead at the end (?![0-9.]) ensures that we don't match a number having a decimal component.

演示

这篇关于正则表达式-仅查找整数-问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆