Can someone show me how to match 4 digits or nothing in Regex? I have been using RegexBuddy and so far i have come up with this but the help text is saying 'Or match regular expression number 2 below - the entire match attempt fails if this one fails to match' in regards to the pattern after the pipe.
I would like either of the two to provide a positive match.
^\d{4}|$
Run Code Online (Sandbox Code Playgroud)
Jer*_*rry 11
无论是4位还是全无:
^(?:\d{4}|)$
Run Code Online (Sandbox Code Playgroud)
这与你想要做的最接近.
我会选择以下稍微更短的一个:
^(?:\d{4})?$
Run Code Online (Sandbox Code Playgroud)
^\d{4}|$
Run Code Online (Sandbox Code Playgroud)
正则表达式意味着:
^\d{4}
要么 $
第一个匹配任何以4位开头的字符串,而第二个将报告所有内容的匹配(它匹配字符串的结尾,因为没有指定字符,它将匹配所有字符串的结尾).
当你使用一个小组时,你会得到:
或者^(?:\d{4})$
或^(?:)$
(注意,OR运算的"限制"改变),然后将其等同于任一^\d{4}$
或^$
归档时间: |
|
查看次数: |
9782 次 |
最近记录: |