How to write a regexp with AND operator?

273 Views Asked by At

I have definition string from my user to match an input string and i would like to simplify the definition string so my user won't need to know regexp internal.

my thought was to allow */-7721/-7722/-7723 to match any given 4 digit string which is not 7721 AND not 7722 AND not 7723.

I am searching for a regexp to perform the above on an input string which is a 4 digit number.

I have tried using the ?! notation, but it can't mis-match the entire string.

(?![0-9]{4}) - this doesn't allow any 4 digit string.

((?!(7721))(?!(7722))(?!(77223)) - this also didn't work

Is there an AND operator to perform the above?

Thanks,

1

There are 1 best solutions below

2
On BEST ANSWER

You forgot ^:

^(?!(?:7721))(?!(?:7722))(?!(?:7723))\d{4}

Edited: added \d{4} for actually matching the string, not just testing