I want to create a regex, where the occurence of one group, depends on whether or not another certain group has been found. I think this is easier to illustrate with an example!
I want to allow two patterns, illustrated by these two examples: JsJh
, JJ
.
This is not allowed: JsJs
, JsJ
, JQ
.
So if the user types one small letter after the first capital one, the regex expects another small letter after the second capital - but it should not be the same letter as the first one!
I match the first (Js) like this: ([123456789TJQKA]){1}([dsch]?){1}
The second capital letter is matched by (\\2)
.
Now, I cannot seem to find a solution for the last small letter. I can get the small letters to match (which is obv. not what I want), but how do I, if its present, exclude the first letter from the last group, by still allowing (and expecting) one of the three remaining small letters?