Reputation: 1145
For example, suppose I want to search for strings beginning with two capital letters:
[A-Z][A-Z]
but I want to exclude strings beginning with 'AB'. How do I incorporate the exclusion into my regular expression?
Upvotes: 2
Views: 2023
Reputation: 227
(?!AB)[A-Z]{2}
explanation:
(?!AB)
: negative lookahead: excludes strings beginning with AB
[A-Z]{2}
: matches 2 capital letters
Upvotes: 3
Reputation: 3323
[A-Z](?!B)[A-Z]
The (?!B)
bit says "match any letter, but not if it's followed by B".
Upvotes: 0
Reputation: 241931
The direct solution:
([B-Z][A-Z]|A[AC-Z])
In other words, if it starts with a capital Ietter other than A, then any second capital will do, but if it starts with an A, the second letter cannot be B so it must be A or in the range C-Z.
Upvotes: 1