Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for a PO Box in all of its forms

We have an C# ASP.Net page where a customer enters in an address where a post office is disallowed as we use UPS for shipping these items. Customers are creative people and they come up with creative means of marking a P.O. Box.

We have this RegEx pattern and it mostly does what we need.

(?i)\b[p]*(?:ost)*\.*\s*[o0]*(?:ffice)*\.*\s+?([b]*[o0]*[x])

This pattern works in almost every case we have on file:

P.O. box 17432
poSt oFFice box 11111
box 222
p0 box 222
#343 po box 
#po box 343

It doesn't match (which is the correct behavior):

1234 Main St (Shouldn't match, but we have it in there for a negative test case.)

However, it also doesn't match these and it should:

p0b 222
POB 1112

These samples are actually values that users have, in their generous nature, provided us with. ;)

I'm always up for simplification.

like image 201
amber Avatar asked Mar 10 '11 18:03

amber


1 Answers

I think this should be close to what you are looking for:

(?i)\b(?:p(?:ost)?\.?\s*[o0](?:ffice)?\.?\s*b(?:[o0]x)?|b[o0]x)

The explanation:

(?:              # start non-capturing group
    p            # match a 'p'
    (?:ost)?     # optionally match 'ost'
    \.?          # optionally match a '.'
    \s*          # match some number of spaces
    [o0]         # match an 'o' or '0'
    (?:ffice)?   # optionally match 'ffice'
    \.?          # optionally match a '.'
    \s*          # match some number of spaces
    b(?:[o0]x)?  # match 'b', 'box', or 'b0x'
  |              # or
    b[o0]x       # match 'box' or 'b0x'
)
like image 83
Andrew Clark Avatar answered Nov 14 '22 08:11

Andrew Clark