这可以在一个正则表达式中完成吗?

时间:2010-12-03 11:29:33

标签: ruby regex

我需要一个正则表达式来匹配一个字符串:

  • 只有数字0-9和空格
  • 所有数字必须相同
  • 应至少有2位数
  • 应以数字开头和结尾

匹配

11
11111
1  1 1 1 1
1  1
11 1 1 1 1 1
1           1
1    1      1

没有匹配:

1             has only one digit
11111         has space at the end
 11111        has space at beginning
12            digits are different
11:           has other character

我知道每个要求的正则表达式。这样我就会使用4个正则表达式 试验。我们可以在一个正则表达式中完成吗?

4 个答案:

答案 0 :(得分:14)

是的,可以在一个正则表达式中完成:

^(\d)(?:\1| )*\1$

Rubular link

说明:

^      - Start anchor
(      - Start parenthesis for capturing
 \d    - A digit
)      - End parenthesis for capturing
(?:    - Start parenthesis for grouping only
\1     - Back reference referring to the digit capture before
|      - Or
       - A literal space
)      - End grouping parenthesis
*      - zero or more of previous match
\1     - The digit captured before
$      - End anchor

答案 1 :(得分:2)

考虑这个程序:

#!/usr/bin/perl -l
$_ = "3 33 3 3";
print /^(\d)[\1 ]*\1$/      ? 1 : 0;
print /^(\d)(?:\1| )*\1$/   ? 1 : 0;

它产生输出

0
1

当您查看已编译的正则表达式时,答案是显而易见的:

perl -c -Mre=debug /tmp/a
Compiling REx "^(\d)[\1 ]*\1$"
synthetic stclass "ANYOF[0-9][{unicode_all}]".
Final program:
   1: BOL (2)
   2: OPEN1 (4)
   4:   DIGIT (5)
   5: CLOSE1 (7)
   7: STAR (19)
   8:   ANYOF[\1 ][] (0)
  19: REF1 (21)
  21: EOL (22)
  22: END (0)
floating ""$ at 1..2147483647 (checking floating) stclass ANYOF[0-9][{unicode_all}] anchored(BOL) minlen 1 
Compiling REx "^(\d)(?:\1| )*\1$"
synthetic stclass "ANYOF[0-9][{unicode_all}]".
Final program:
   1: BOL (2)
   2: OPEN1 (4)
   4:   DIGIT (5)
   5: CLOSE1 (7)
   7: CURLYX[1] {0,32767} (17)
   9:   BRANCH (12)
  10:     REF1 (16)
  12:   BRANCH (FAIL)
  13:     EXACT < > (16)
  15:   TAIL (16)
  16: WHILEM[1/1] (0)
  17: NOTHING (18)
  18: REF1 (20)
  20: EOL (21)
  21: END (0)
floating ""$ at 1..2147483647 (checking floating) stclass ANYOF[0-9][{unicode_all}] anchored(BOL) minlen 1 
/tmp/a syntax OK
Freeing REx: "^(\d)[\1 ]*\1$"
Freeing REx: "^(\d)(?:\1| )*\1$"

Backrefs只是字符类中的常规八进制字符!!

答案 2 :(得分:1)

^(\d)( *\1)+$


答案 3 :(得分:0)

/^(\d)(\1| )*\1$/