Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RegEx – match where second letter is not first letter and third letter is first letter

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading