Alex
Alex

Reputation: 59

pcre2 - Regex match to distinct letters only

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

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.

Upvotes: 2

Views: 625

Answers (2)

The fourth bird
The fourth bird

Reputation: 163372

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

Another idea if you are using Javascript and you can not use (?i) is to use 2 patterns, 1 for checking not a repeated character with a case insensitive flag /i and 1 for the full match.

const rFullMatch = /^[A-Z][a-z]{5}\.com$/;
const rRepeatedChar = /^[a-z]*([a-z])[a-z]*\1/i;
[
  "Regxas.com",
  "Ipsamo.com",
  "Plpaso.com",
  "Amipsa.com",
  "Ipsama.com",
  "Ipsima.com",
  "IPszma.com",
  "Ipsamo&.com",
  "abcdef.com"
].forEach(s => {
  if (!rRepeatedChar.test(s) && rFullMatch.test(s)) {
    console.log(`Match: ${s}`);
  }
});

Upvotes: 2

anubhava
anubhava

Reputation: 785296

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

Upvotes: 1

Related Questions