Reputation: 25
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
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
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 Word
s, 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.Upvotes: 1