开发者

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开发者_StackOverflow中文版 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.


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'
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜