Reputation: 14632
I am trying to write a regular expression matching a set without some chars.
For example, it matches [ a-zA-Z]*
but excludes i,o,q,I,O,Q
.
So: "A fat cat" matches, "Boy" doesn't.
Looks like it can be [ a-hj-npr-zA-HJ-NPR-Z]*
.
Is there a simpler version for this?
Btw, I'm using it in PostgreSQL
, but I think it should be a standard expression.
Upvotes: 0
Views: 261
Reputation: 784888
You can use negative lookahead for this as Postgresql support lookaheads:
(?![ioqIOQ])[A-Za-z ]
To make it match complete line use:
^(?:(?![ioqIOQ])[A-Za-z ])+$
Upvotes: 1
Reputation: 13060
Don't need fancy lookaheads/behinds just use more, but smaller, character ranges.
You'll want something like ^[a-hj-npr-zA-HJ-NPR-Z ]*$
.
Added a space to match sentences
You can see test this on-line here at debuggex
Upvotes: 1
Reputation: 189307
Based on @Anubhava's answer, but extending to an entire string rather than just one character,
^(?=[^ioqIOQ]*$)[ A-Za-z]*$
The (?=...)
is a positive lookahead -- the opposite of the negative lookahead in Anubhava's answer. We are requiring all matches to also match the constraint [^ioqIOQ]
.
You could also implement the repetition over the entire string with
^((?![ioqIOQ])[ A-Za-z])*$
but it seems a lot less efficient. (I have not performed any timings, though.)
Upvotes: 1