简体   繁体   English

正则表达式为以0开头但不是十进制的数字

[英]regex for number that starts with 0 but is not a decimal

I am trying to write a regex for a number that starts with 0 but shouldn't match for decimals eg the regex should match 00909,050,000033 but shouldn't match 0.909 . 我正在尝试为以0开头但不适合小数的数字编写正则表达式,例如正则表达式应匹配00909,050,000033但不匹配0.909 This is the regex I used: 这是我使用的正则表达式:

/[0]+\d+(?!\.)/g 

It is flawed because it matches 0.909 which is not what I want. 这是有缺陷的,因为它匹配我不想要的0.909 Also, if I have a string like 0009+045*0.909 , the regex should match 0009 and 045 but not 0.909 . 另外,如果我有一个字符串,如0009+045*0.909 ,则正则表达式应匹配0009045但不能匹配0.909

I appreciate anyone with a solution for this. 非常感谢有人为此提供解决方案。

You can use the following regex to achive what you wanted, 您可以使用以下正则表达式实现所需的功能,

/^0[0-9]+/g

The above will select a number in this pattern, 上面将以此模式选择一个数字,

  • string should start with 0 . 字符串应以0开头。
  • And followed by 1 or more continuous numbers. 然后是1个或多个连续数字。

And the above mentioned regex can also be written as, 上述正则表达式也可以写成

/^0\d+/g
/\D*(0\d+?)\D*/g

这将提取模式的所有实例,这些实例可作为$ 1 ... $ 9使用

You can try to add a word boundary to the start of your pattern ( demo ): 您可以尝试在模式的开头( demo )添加单词边界:

/\b0+\d+\b(?!\.)/g
 ^^

See the regex demo. 参见正则表达式演示。 However, it will also fail to match numbers at the end of a sentence before the final dot. 但是,它也不会在最后一个点之前的句子末尾匹配数字。 Then, use a more precise lookahead to fail matches before a . 然后,使用更精确的超前功能使之前的匹配失败. followed with a digit: 后面跟一个数字:

/\b0+\d+\b(?!\.\d)/g
         ^^^^^^^^ 

See this regex demo 观看此正则表达式演示

But the safest way (in case the number looks like 4.05 ) is to use a more sophisticated pattern (to be used with RegExp#exec() in a loop to extract match[1] value): 但是最安全的方法 (如果数字看起来像4.05 )是使用更复杂的模式(与RegExp#exec()在循环中一起使用,以提取match[1]值):

/(?:^|[^\d.])(0+\d+)\b(?!\.\d)/g

See the regex demo 正则表达式演示

Details: 细节:

  • (?:^|[^\\d.]) - non-capturing group matching the start of string or a non-digit and non-dot character (?:^|[^\\d.]) -与字符串开头或非数字和非点字符匹配的非捕获组
  • (0+\\d+)\\b - 1+ zeros followed with 1+ digits (a whole word) (0+\\d+)\\b -1+个零后跟1+个数字(整个单词)
  • (?!\\.\\d) - not followed with a . (?!\\.\\d) -后面没有. + a digit. +一个数字。

Code: 码:

 var re = /(?:^|[^\\d.])(0+\\d+)\\b(?!\\.\\d)/g; var str = 'should match \\n00909\\n050\\n000033\\n0009\\n045\\n0009+045\\n but shouldn\\'t match\\n0.909\\n4.05'; var res = []; while ((m = re.exec(str)) !== null) { res.push(m[1]); } document.body.innerHTML += "<pre>" + str + "</pre>"; document.body.innerHTML += "<pre>" + JSON.stringify(res, 0, 4) + "</pre>"; 

All you have to do is constrain the boundaries from not matching a 您要做的就是限制边界不匹配
dot nor digit before and after. 点和数字之前和之后。 The middle will then be valid. 中间将是有效的。

Answer in capture group 1. 在捕获组1中回答。

(?:^|[^\\d.])(0\\d+)(?![\\d.])

 (?: ^ | [^\d.] )          # BOS or not digit nor dot
 ( 0 \d+ )                 # (1), Number that starts with 0
 (?! [\d.] )               # Not digit nor dot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM