Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How to negate PO BOX - Regex

I've tried a few things, haven't been able to get it to work. I need to exclude PO Boxes. I thought I just had to wrap it with ?!..but it is not working. Any thoughts?


EDIT: Sorry, this is what I am looking for.

Example: when input is "P.O. BOX" or "Post Office" I need regex to be false. When input is 7821 Test street, I need regex to be true.

I am attempting to use it in an ASP.net MVC project

/// <summary>
/// Regex for street fields
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple =     false)]
public class StreetAddressAttribute : RegularExpressionAttribute, IClientValidatable
/// <summary>
/// Regular expression validation for street field
/// </summary>
public StreetAddressAttribute()
  : base(@"^(?!(?i)[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\s*(\d.)*)$")

 /// <summary>
 /// Client side validation
 /// </summary>
 /// <param name="metadata">Modelmetadata</param>
 /// <param name="context">ControllerContext</param>
 /// <returns>Client-side validation rules</returns>
 public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata    metadata, ControllerContext context)
   yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "streetaddress" };

Thanks for your help

like image 744
Ricka Avatar asked Mar 23 '23 20:03


1 Answers

Your regex has so many issues, I try to address one after the other

  1. 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]

  2. You use the inline modifier (?i). This makes the following letters all matched case independent. So [Pp] is unnecessary, just p is enough to match the letters "P" and "p".

    Including this first two issues we can change your expression to

  3. 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:


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
like image 139
stema Avatar answered Apr 05 '23 21:04