Home > OS >  pcre2 - Regex match to distinct letters only
pcre2 - Regex match to distinct letters only

Time:05-12

I am trying to create a regex that matches the following criteria below

  • first letter is uppercase
  • remaining 5 letters following the first letter are lowercase
  • ends in ".com"
  • no letter repeats only before the ".com"
  • no digits
  • there are only 5 lowercase letters before the .com, with only the first letter being uppercase

The above criteria should match to strings such as: Amipsa.com Ipsamo.com

I created this regex below, but the regex seem to capture repeating letters - examples here: https://regex101.com/r/wwJBmc/1

^([A-Z])(?![a-z]*\1)(?:([a-z])\1(?!\2)(?:([a-z])(?![a-z]*\3)){3}|(?:([a-z])(?![a-z]*\4)){5})\.com$

Would appreciate any insight.

CodePudding user response:

You may use this regex in PCRE with a negative lookahead:

^(?!(?i)[a-z]*([a-z])[a-z]*\1)[A-Z][a-z]{5}\.com$

Updated RegEx Demo

RegEx Details:

  • ^: Start
  • (?!: Start negative lookahead
    • (?i): Enable ignore case modifier
    • [a-z]*: Match 0 or more letters
    • ([a-z]): Match a letter and capture in group #1
    • [a-z]*: Match 0 or more letters
    • \1: Match same letter as in capture group #1
  • ): End negative lookahead
  • [A-Z][a-z]{5}: Match 5 lowercase letters
  • \.com: Match .com
  • $: End

CodePudding user response:

You might use

^(?i)[a-z]*?([a-z])[a-z]*?\1(?-i)(*SKIP)(*F)|[A-Z][a-z]{5}\.com$

Regex demo

Explanation

  • ^ Start of string
  • (?i) Case insensitive match
  • [a-z]*?([a-z])[a-z]*?\1 Match 2 of the same chars a-z A-Z
  • (?-i) Turn of case insenstive
  • (*SKIP)(*F) Skip the match
  • [A-Z][a-z]{5} Match A-Z and 5 chars a-z
  • \.com Match .com
  • $ End of string
  • Related