Gre*_*reg 91
/^([^,]*,){21}[^,]*$/
Run Code Online (Sandbox Code Playgroud)
那是:
^ Start of string
( Start of group
[^,]* Any character except comma, zero or more times
, A comma
){21} End and repeat the group 21 times
[^,]* Any character except comma, zero or more times again
$ End of string
Run Code Online (Sandbox Code Playgroud)
Pet*_*ton 11
如果您正在使用支持占有量词的正则表达式(例如Java),您可以:
^(?:[^,]*+,){21}[^,]*+$
Run Code Online (Sandbox Code Playgroud)
占有量词可以比贪心量词更好.
说明:
(?x) # enables comments, so this whole block can be used in a regex.
^ # start of string
(?: # start non-capturing group
[^,]*+ # as many non-commas as possible, but none required
, # a comma
) # end non-capturing group
{21} # 21 of previous entity (i.e. the group)
[^,]*+ # as many non-commas as possible, but none required
$ # end of string
Run Code Online (Sandbox Code Playgroud)
正好21个逗号:
^([^,]*,){21}[^,]$
Run Code Online (Sandbox Code Playgroud)
至少21个逗号:
^([^,]?,){21}.*$
Run Code Online (Sandbox Code Playgroud)