Carlos Valls
Carlos Valls

Reputation: 25

How to build a regex for a string that needs multiple validations

I'm trying to create some regular expressions for a project that I'm currently developing.

I need to know how to create a regex that matches a string with the following pattern:

ABC Word Word

  1. I need a 3 letter uppercase caps prefix at the start [A-Z]
  2. All words that some next have to be spaced with a blank space and they need to start capitalized.
  3. The string can not contain PM or MP as a secondary prefix. (ABC PM Paragraph) This applies for any position.

I've come up with this regex:

(^[A-Z]{3})(.* [A-Z][a-z]{1,})

It works correctly with a string like : "ABC Create User" but it also matches "ABCDE Create User" , "ABC CreAte User" , etc.

Upvotes: 1

Views: 148

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

Note that .* matches any 0 or more characters other than line break chars, as many as possible, and this pattern is rarely useful when validating strings.

You may use

^[A-Z]{3} (?!(?:MP|PM)\b)[A-Z][a-z]+ (?!(?:MP|PM)\b)[A-Z][a-z]+

Or, using a non-capturing group with a limiting quantifier:

^[A-Z]{3}(?: (?!(?:MP|PM)\b)[A-Z][a-z]+){2}
         ^^^                           ^^^^ 

See the regex demo #1 and regex demo #2.

If you need to match two or more of such Words, use

^[A-Z]{3}(?: (?!(?:MP|PM)\b)[A-Z][a-z]+){2,}
                                        ^^^^

The (?!(?:MP|PM)\b)[A-Z][a-z]+ part does the following:

  • (?!(?:MP|PM)\b) negative lookahead checks if the word is equal to MP or PM, and fails the match if the pattern matches
  • [A-Z][a-z]+ consumes an uppercase letter followed with 1+ lowercase ones.

enter image description here

Upvotes: 1

Related Questions