[英]Regex to match exactly one comma separeted strings
Example:例子:
src: anyword, anyword, anyword
I want to check if src
contains only one comma
separated strings like given in example我想检查src
是否只包含one comma
分隔的字符串,如示例中给出的
I wrote (.[^\\s*]+,.*[^,\\s*]+$)
this but it's failing with some scenarios我写了(.[^\\s*]+,.*[^,\\s*]+$)
但这在某些情况下失败了
For me, following scenarios are not valid对我来说,以下场景无效
src: abc, ,1232
src: ,abc, dasf
demo: https://regex101.com/r/UyQ5KX/1演示: https : //regex101.com/r/UyQ5KX/1
You may use您可以使用
: *[^\s,]+(?:, *[^,\s]+)*$
Or, to allow more whitespaces than just space between words:或者,允许更多的空格而不仅仅是单词之间的空格:
:[^\S\n\v\f\r\u2028\u2029]*[^\s,]+(?:,[^\S\n\v\f\r\u2028\u2029]*[^,\s]+)*$
See the regex demo查看正则表达式演示
If there must be 1+ non-colon chars from the start of string till the colon, you may add ^[^:]+
before the patterns above.如果从字符串的开头到冒号必须有 1+ 个非冒号字符,则可以在上述模式之前添加^[^:]+
。
Details细节
^
- start of string ^
- 字符串的开始[^:]+
- 1+ chars other than :
[^:]+
- 1+ 个字符,除了:
:
- a colon :
- 一个冒号[^\\S\\n\\v\\f\\r\
\
]*
- 0 or more occurrences of only horizontal whitespaces [^\\S\\n\\v\\f\\r\
\
]*
- 仅出现 0 次或多次水平空格[^\\s,]+
- 1 or more occurrences of characters other than whitespace and comma [^\\s,]+
- 出现 1 次或多次除空格和逗号以外的字符(?:,[^\\S\\n\\v\\f\\r\
\
]*[^,\\s]+)*
- 0 or more occurrences of (?:,[^\\S\\n\\v\\f\\r\
\
]*[^,\\s]+)*
- 0 次或多次出现
,
- a comma ,
- 逗号[^\\S\\n\\v\\f\\r\
\
]*
- 0 or more occurrences of only horizontal whitespaces [^\\S\\n\\v\\f\\r\
\
]*
- 仅出现 0 次或多次水平空格[^\\s,]+
- 1 or more occurrences of characters other than whitespace and comma [^\\s,]+
- 出现 1 次或多次除空格和逗号以外的字符$
- end of string. $
- 字符串的结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.