如何创建一个RegExp只获取整数而不是小数 [英] How to create a RegExp to get only integer numbers and no decimals
问题描述
嘿伙计们,
目前我将尝试创建一个与字符串中的整数值匹配的RegExp。
所以我没有这样的字符串:
123
或 123.456
如果是这种情况,我可以简单地使用 ^ [\d] * $
作为RegExp。
但就像我说的那样,我将尝试在字符串中获取整数值。
两个示例字符串可能如下所示:
示例整数值例如是123456美元< br />
其他一些小数值例如是123.456美元
目标是,RegExp只能在第一个字符串中匹配 123456
而不是 123
或 456
在第二个字符串中,因为第二个字符串包含一个十进制值,而不是两个用点分隔的整数值。
我希望我能说清楚我的意思。
如果没有,请随时询问。
非常感谢你的帮助!
编辑:
我玩过RegExp并找到了以下两个RegExp:
[\d] +(?!\。)
所有数字但没有数字是一个点
和
(?<!\。)[\ d] +
所有数字但点后没有数字
但是我很困惑如何将它们与逻辑和操作连接起来只得到匹配的地方没有点bevore数字和数字后没有点。
作为RegExp。
但就像我说的那样,我要去尝试在字符串中获取整数值。
两个示例字符串可能如下所示:
示例整数值例如为123456 Dollar< br / >
其他一些十进制值例如是123.456美元
目标是,RegExp只能匹配123456
在第一个字符串中,而不是在123
或456
在第二个字符串中,因为第二个字符串包含一个十进制值,而不是两个用点分隔的整数值。
我希望我能说清楚我的意思。
如果没有,请随时询问。
非常感谢你的帮助!
编辑:
我玩过RegExp,发现以下两个RegExp:
[\d] +(?!\。)
所有数字但没有数字是一个点
和
(?<!\。)[\ d] +
所有数字但点后没有数字
但是我很困惑如何将它们与逻辑和操作连接起来只得到匹配的地方没有点bevore数字和数字后没有点。
< blockquote>这是一个正如你想要的正则表达式:
\ b(?<!\。)\ d +(?!\。\ d )\ b
\ b
是零长度锚,匹配单词边界。它确保以下数字不是abc123之类的单词的一部分。
(?<!\。 )
是一个负面的lookbehind,确保你想要的数字前面没有小数
(?!\ 。\d)
是一个负向前瞻,确保您想要的数字后面没有小数和数字
编辑: 添加了lookbehind以防止匹配小数部分
Hey guys,
at the moment im going to try to create a RegExp that matchs to integer values in strings.
So i don't have strings like this:
"123
" or "123.456
"
If that would be the case i could simply use "^[\d]*$
" as RegExp.
But like i said, im going to try to get integer values in strings.
Two sample strings could look like this:
A sample integer value is for example 123456 Dollar<br /> Some other decimal value is for example 123.456 Dollar
The goal is, that the RegExp only get a matches the 123456
in the first string and not at 123
or 456
in the second one, because the second string contains a decimal value, not two integer values separated by a dot.
I hope i could make clear what i mean.
If not, feel free to ask.
Thank you so much for your help!
Edit:
I've played with RegExp and found the following two RegExp:
[\d]+(?!\.)
all numbers but no number bevore a dot
and
(?<!\.)[\d]+
all numbers but no number after a dot
But i'm confused how to connect them with a logical and operation to get only matches where is no dot bevore a number AND no dot after a number.
" as RegExp.
But like i said, im going to try to get integer values in strings.
Two sample strings could look like this:
A sample integer value is for example 123456 Dollar<br /> Some other decimal value is for example 123.456 Dollar
The goal is, that the RegExp only get a matches the123456
in the first string and not at123
or456
in the second one, because the second string contains a decimal value, not two integer values separated by a dot.
I hope i could make clear what i mean.
If not, feel free to ask.
Thank you so much for your help!
Edit:
I've played with RegExp and found the following two RegExp:
[\d]+(?!\.)
all numbers but no number bevore a dot
and
(?<!\.)[\d]+
all numbers but no number after a dot
But i'm confused how to connect them with a logical and operation to get only matches where is no dot bevore a number AND no dot after a number.
Here's a regex that will do what you want:
\b(?<!\.)\d+(?!\.\d)\b
\b
is a zero-length anchor, matching a "word" boundary. It ensures that the following digits aren't part of a "word" like "abc123".
(?<!\.)
is a negative lookbehind that ensures there is no decimal preceding the digits you want
(?!\.\d)
is a negative lookahead that ensures there is no decimal and digit following the digits you want
EDIT: added lookbehind to prevent matching part of a decimal
这篇关于如何创建一个RegExp只获取整数而不是小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!