I'm looking for a regex that will identify expressions where the first and third letters are the same, but different to the second letter.
e.g. match against the following expressions:
abaxyz
bzbaaadsfsdf
but not
aaaxyz
abcdefg
bbbaaasdf
I've tried back-references, but can't make "NOT match 1"
Imagining something like ^(?[a-z]){!P!}{P1}
where P1 is the capture of the first letter, {!P1} is "anything except the first capture" and {P1} is "same as the first capture".
I have more complex requirements to follow, such as "first letter, not first letter, not first or second letter" (i.e. 3 distinct letters at the start)
e.g.
abcdef
but not
abbxyzz
So if anyone can point me to the proper regex constructs for referencing and matching/excluding previous matches/captures, that would much appreciated. Thanks.
CodePudding user response:
You can use
^([a-z])(?!\1)[a-z]\1
See the regex demo. Details:
^
- start of string([a-z])
- Group 1: a lowercase ASCII letter(?!\1)
- a negative lookahead that fails the match if there is Group 1 value immediately to the right of the current location[a-z]
- a lowercase ASCII letter\1
- (an inline backreference) Group 1 value.
If you want to match the rest of the word, append the [a-z]*$
to the regex.