我需要一个正则表达式匹配一个字符串:
0-9和空格火柴:
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个正则表达式测试.我们可以在一个正则表达式中完成吗?
cod*_*ict 14
是的,它可以在一个正则表达式中完成:
^(\d)(?:\1| )*\1$
Run Code Online (Sandbox Code Playgroud)
说明:
^ - 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
174 次 |
| 最近记录: |