Reputation: 26
I am trying to create a regex that matches when there are no repeating characters of a particular type and all other characters are ignored. Word length does not matter. Ex.
hippos-R-Y-S <--- Matches
hippos-R-Y-Y <--- Does not match
hippos-R-Y-P <--- Matches
Again, the hippos-
text could be anything, but the capital letters that follow have to be in the set of [YRPS]
. Thanks for your help!!
Upvotes: 0
Views: 1147
Reputation: 784938
You can use this regex based on 2 negative lookaheads to skip matching repeated character in [YRPS]
class:
^[^-]+-([YRPS])(?:-(?!\1)([YRPS])(?!.*\2))+$
**RegEx Breakup:*
^ # line start
[^-]+ # match 1 or more of any char that is not a -
- # match literal -
([YRPS]) # match [YRPS] and group it #1
(?: # start non-capturing group
- # match literal -
(?!\1) # negative lookahead to assert next char is not same as group #1
([YRPS]) # match [YRPS] and group it #2
(?!.*\2) # negative lookahead to assert next char is not same as group #2
)+ # end non-capture group and + makes it match 1 or more of the same set
$ # end of line
Upvotes: 1
Reputation: 91375
This should do the job:
^.*?-([YRPS])-(?!\1)([YRPS])-(?!\1)(?!\2)([YRPS])$
Explanation:
^ : begining of string
.*?- : 0 or more any char until dash
([YRPS]) : one of the set, captured in group 1
- : a dash
(?!\1) : negative lookahead, not the same letter as in group 1
([YRPS]) : one of the set, captured in group 2
- : a dash
(?!\1) : negative lookahead, not the same letter as in group 1
(?!\2) : negative lookahead, not the same letter as in group 2
([YRPS]) : one of the set, captured in group 3
$ : end of string
Upvotes: 1