mikolajek
mikolajek

Reputation: 91

Regex evaluating three letter construction

I'm trying to build a regex that would tell me if 3-letter string (being document series) is valid. Basically everything from "aaa" to "ard" should be valid and everything starting from "are" needs to be deemed invalid. I got stuck with the condition as I can't really figure out how to build it. I've tried the one below, but it returns all "ar*" as valid, even though all "as*" and higher are considered invalid.

[a]{1}[a-r]{1}?[a-z](?(1)<=([s-z])([\Z])){1}

Would you please help me putting the correct code in place?

Upvotes: 2

Views: 24

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You may use

^a(?:[a-q][a-z]|r[a-d])$

See the regex demo

Details

  • ^ - start of a string
  • a - a a letter
  • (?:[a-q][a-z]|r[a-d]) - either of the two alternatives:
    • [a-q][a-z] - a letter from a to q followed with any ASCII lowercase letter
    • | - or
    • r[a-d] - r followed with a letter from a to d
  • $ - end of string.

Upvotes: 2

Related Questions