简体   繁体   English

正则表达式匹配数字和它们之间最多一个空格

[英]Regex to match digits and at most one space between them

I am trying to match phone numbers in the following patterns: 我想尝试以下列模式匹配电话号码:

9 99 99 99 99
0999999999
11 0999999999
9 9999 9999

But not the following: 但不是以下内容:

9 99  99 99 99 (two spaces)
9 99\n99 99 99 

Therefore, I want to match 7 to 12 digits and an optional spaces between them, but not sequences of more than one white space. 因此,我想匹配7到12位数字和它们之间的可选空格,但不是多个空格的序列。

So far I came up with "[\\d ?]{7,12}" , but it doesn't really match the requirements as the spaces are counted in the {7,12} and it also matches two sequences of spaces. 到目前为止,我想出了"[\\d ?]{7,12}" ,但它并没有真正符合要求,因为空间在{7,12}中计算,它也匹配两个空格序列。

[\\d ?]{7,12} is a pattern that matches 7 to 12 digit, space or ? [\\d ?]{7,12}是一个匹配7到12位,空格或? chars. 字符。 It can match a ??????? 它可以匹配??????? string because ? 字符串因为? is not a quantifier, but a mere question mark symbol when declared inside a character class. 不是量词,而是在字符类中声明时仅仅是问号标记。

If you change it to (?:\\d ?){7,12} , you may partially solve the problem, the space at the end. 如果将其更改为(?:\\d ?){7,12} ,则可以部分解决问题,即最后的空格。 I suggest using 我建议使用

\b\d(?: ?\d){6,11}\b

See the regex demo 请参阅正则表达式演示

The word boundaries \\b will make sure you only match whole words. 单词boundary \\b将确保您只匹配整个单词。

Details 细节

  • \\b - leading word boundary \\b - 领先的单词边界
  • \\d - a digit \\d - 一个数字
  • (?: ?\\d){6,11} - 6 to 11 consecutive sequences of (?: ?\\d){6,11} - 6到11个连续序列
    • ? - an optional space - 可选空间
    • \\d - a single digit \\d - 单个数字
  • \\b - trailing word boundary. \\b - 尾随字边界。

I'd try 我试试

(?:\d+ ?){7,12}

The original regex was matching a character group of a space OR a digit seven to twelve times. 原始正则表达式匹配空格的字符组或数字七到十二次。 The supplied regex matches a digit followed by a possible space seven to twelve times. 提供的正则表达式匹配一个数字,后跟一个可能的空格七到十二次。 That way the spaces aren't counted as part of the total. 这样,空间不算作总数的一部分。

you can use 您可以使用

\d(\s?\d){6,11}

the first \\d matches on the first digit. 第一个\\d匹配第一个数字。 Next can follow a group of 6 to 11 (to make a total of 7 to 12) pairs of an optional space, followed by a digit. 接下来可以跟随一组6到11(总共7到12对)可选空格,然后是数字。 Multiple spaces are not allowed, as you see each optional space has digits to both sides. 不允许有多个空格,因为您看到每个可选空格都有两边的数字。 It can be checked here That regexp is equivalent, but shorter, to this one: 可以在这里检查regexp与此相同,但更短:

\d\s?\d\s?\d\s?\d\s?\d\s?\d((((((\s?\d)?\s?\d)?\s?\d)?\s?\d)?\s?\d)?\s?\d)?

that can be checked here . 这可以在这里查看

NOTE 注意

See that the \\s matches a newline, so you can get multiline number (as shown in the examples) If you don't like that behaviour, then narrow the space class using a simple space, as in 看到\\s匹配换行符,因此您可以获得多行数(如示例中所示)如果您不喜欢该行为,则使用简单空格缩小空间类,如

\d( ?\d){6,11}

that can be tested here Look, that now, a more than 12 digits number is truncated to only the first twelve, if this is not desired, use word boundary at the end, as in 可以在这里测试看看,现在,超过12位的数字被截断为只有前12位,如果不需要,最后使用字边界,如

\d( ?\d){6,11}\b

See it here . 在这里看到它。

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

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