[英]Unique sequence of similar characters in a string using regex
I have some test strings:我有一些测试字符串:
I want to make a regex which should match strings 1,2,3,4 but not 5.我想制作一个应该匹配字符串 1,2,3,4 但不是 5 的正则表达式。
So my constraints for match are:所以我对比赛的限制是:
Note:- I want only one regex to solve this problem.注意:- 我只想要一个正则表达式来解决这个问题。 Because with separate regex and iteration I have already done it.
因为使用单独的正则表达式和迭代我已经完成了。 I am searching for single line solution to validate my string.
我正在寻找单行解决方案来验证我的字符串。
The solution I have tried is:我尝试过的解决方案是:
/(?=^[xym\W]+$)((?=^([^m]*\W)?m+(\W[^m]*)?$)|(?=^([^x]*\W)?x+(\W[^x]*)?$)|(?=^([^y]*\W)?y+(\W[^y]*)?$))/
But it is matching 5th case also.但它也匹配第 5 种情况。
You may use您可以使用
/^(?!.*\b([xym])\1*\b.*\b\1+\b)(?:\s*\b([xym])\2*\b)*\s*$/
See the regex demo .请参阅正则表达式演示。
Details细节
^
- start of string ^
- 字符串的开始(?!.*\\b([xym])\\1*\\b.*\\b\\1+\\b)
- a negative lookahead that fails the match if immediately after the string start there is (?!.*\\b([xym])\\1*\\b.*\\b\\1+\\b)
- 如果在字符串开始后立即出现匹配失败的负前瞻
.*
- any 0+ chars other than line break chars, as many as possible .*
- 除换行符以外的任何 0+ 个字符,尽可能多\\b([xym])\\1*\\b
- a whole word that consists of identical chars, x
, y
or m
\\b([xym])\\1*\\b
- 由相同字符组成的整个单词, x
, y
或m
.*
- any 0+ chars other than line break chars, as many as possible .*
- 除换行符以外的任何 0+ 个字符,尽可能多\\b\\1+\\b
- a whole word that consists of a char captured in Group 1 \\b\\1+\\b
- 由第 1 组中捕获的字符组成的整个单词(?:\\s*\\b([xym])\\2*\\b)*
- 0 or more repetitions of (?:\\s*\\b([xym])\\2*\\b)*
- 0 次或多次重复
\\s*
- 0 or more whitespace chars \\s*
- 0 个或多个空白字符\\b([xym])\\2*\\b
- a whole word that consists of 1 or more of the same chars, x
, y
or m
\\b([xym])\\2*\\b
- 由 1 个或多个相同字符x
、 y
或m
组成的整个单词\\s*
- 0 or more whitespace chars \\s*
- 0 个或多个空白字符$
- end of string. $
- 字符串的结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.