Your regex has so many issues, I try to address one after the other
In a Character class you don't need a
|
as an OR. Every character inside is added to the allowed characters list. So doing a[P|p]
allows the three characters "P", "p" and "|".The correct class would be
[Pp]
You use the inline modifier
(?i)
. This makes the following letters all matched case independent. So[Pp]
is unnecessary, justp
is enough to match the letters "P" and "p".Including this first two issues we can change your expression to
^(?!(?i)p*(ost)*\.*\s*[Oo0]*(ffice)*\.*\s*b[o0]x\s*(\d.)*)$
You made everything except
b[o0]x
repeatable 0 or more times by the*
quantifier. I am quite sure this is not what you want, or do you want to find things like "pppppppostostb0x"?
The regex that is false, when the input is "P.O. BOX" or "Post Office" is more like this:
^(?i)(?!p\.?o\.?\sbox|post\soffice).*$
This regex would match every string (because of the .*
at the end), except strings that start like this like this:
- po box
- p.o box
- p.o. box
- Post Office
- POST oFfIcE