Dave
Dave

Reputation: 11

Regular Expression to match sequences of one or more letters except for a specific value

Looking for some help with a Regular Expression to do the following:

Thanks for any help, Dave

Upvotes: 1

Views: 106

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386030

Solve this in two steps:

  1. compare against the regular expression [a-zA-Z]+ which means "one or more of the letters from a-z or A-Z
  2. if it passes that test, look it up in a list of specific values you are guarding against.

There's no point in trying to cram these two tests into a single complex regular expression that you don't understand. A good rule of thumb with regular expressions is, if you have to ask someone how to do it, you should strive to use the least complex solution possible. If you don't understand the regular expression you won't be able to maintain the code over time.

In pseudocode:

if regexp_matches('[a-zA-Z]+', string) && string not in ['Default', 'Foobar', ...] {
    print "it's a keeper!"
}

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838416

Use a negative lookahead:

^(?!Default)[a-zA-Z]+$

Upvotes: 2

Related Questions