user2408578
user2408578

Reputation: 464

How to negate this regex

The following regex will do validation for the P. O. Box is entered in the text box,

\b[P|p](OST|ost)?[.\s-]+[O|o](FFICE|ffice)?[.\s-]+[B|b](OX|ox)\b

i want to negate this so as to detect whether user is not entering P. O. Box it in the text box, I know that we can do it by using javascript also, but my platform has different form structure, its demandware form, where i have regex as the field attribute. We can submit a regex in this field and it will validate it automatically.Any idea?

Upvotes: 0

Views: 472

Answers (4)

instanceof me
instanceof me

Reputation: 39198

If the regex flavor is JavaScript's, then you can use negative look-ahead:

^(?!.*?\b[Pp](OST|ost)?[.\s-]+[Oo](FFICE|ffice)?[.\s-]+[Bb](OX|ox)\b)

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

You can use this pattern:

^(?:[^p]+|\Bp+|p(?!(?:ost)?[.\s-]+o(?:ffice)?[.\s-]+box\b))+$

The idea is to test only substrings that begin with "p" (for more performances). To make this check case insensitive, you can add (?i) at the begining of the pattern:

^(?i)(?:[^p]+|\Bp+|p(?!(?:ost)?[.\s-]+o(?:ffice)?[.\s-]+box\b))+$

Upvotes: 0

jason
jason

Reputation: 241731

I recommend not attempting to do this with a regular expression. It is way too easy to defeat1. Instead, you need to out source the problem to someone who knows what they are doing. In this case, since you are dealing with US addresses only, the USPS.

So, you should use the USPS address standarization/verification API. You can submit an address, and it will return to you a "cleaned" version of that address. It will tell you whether or not the address is valid. And if it is a post office box, it will return it to you in a standardized format and now you don't need a regular expression that can be defeated, now you only need a simple string match. And, as an added bonanza, you'll get a standardized and validated representation of the delivery address reducing2 the possibility of error.

I recognize I am sidestepping your actual engineering question. But part of engineering is abandoning solutions that are the wrong path. You need to validate addresses. So validate addresses rather than trying to build a state machine that can detect some inputs that represent post office boxes but will fail on others. And the USPS provides a validation service and they are the authoritative experts here.

1: I am not saying that you'll face adversaries, just you'll face all the creative, sloppy, lazy ways that people have for entering in their addresses.

2: But not eliminating.

Upvotes: 3

Dancrumb
Dancrumb

Reputation: 27549

You need to use a negative lookaround: (?!pattern).

In this case

 (?! \b[P|p](OST|ost)?[.\s-]+[O|o](FFICE|ffice)?[.\s-]+[B|b](OX|ox)\b )

For reference:

Regex lookarounds

Upvotes: 0

Related Questions