简体   繁体   中英

Unique sequence of similar characters in a string using regex

I have some test strings:

  1. "x"
  2. " mm "
  3. "x mm"
  4. "yy x mm"
  5. "xx mm y mm"

I want to make a regex which should match strings 1,2,3,4 but not 5.

So my constraints for match are:

  1. One alphabet sequence should occur once in the string. (eg "y" is a sequence one y and "yy" is a sequence of two y's but they contain same alphabet so they are contradictory and can't occur together)
  2. Only specific alphabets are allowed in the string (for my case "xym").
  3. Any sequence can occur at start, middle or end of the string. But it should be prefixed or suffixed with non-word character if another alphabet sequence precedes or succeeds it respectively.
  4. It is not necessary that all the alphabet sequences must present in the string.

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.

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
    • .* - any 0+ chars other than line break chars, as many as possible
    • \\b([xym])\\1*\\b - a whole word that consists of identical chars, x , y or m
    • .* - any 0+ chars other than line break chars, as many as possible
    • \\b\\1+\\b - a whole word that consists of a char captured in Group 1
  • (?:\\s*\\b([xym])\\2*\\b)* - 0 or more repetitions of
    • \\s* - 0 or more whitespace chars
    • \\b([xym])\\2*\\b - a whole word that consists of 1 or more of the same chars, x , y or m
  • \\s* - 0 or more whitespace chars
  • $ - end of string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM