I have the following regex from regex lib that does a fine job to catch some PO Box variations. The only thing it does not do is to match trailing digits:
PO Box 123
Can anyone please help modifying this regex to also match trailing digits?
\b[P|p]?(OST|ost)?\.?\s*[O|o|0]?(ffice|FFICE)?\.?\s*[B|b][O|o|0]?[X|x]?\.?\s+[#]?(\d+)\b
Thanks.
Assuming the trailing digits in “PO Box 123” are “123”, the regex does match them. It even captures the number to backreference 3.
There are a large number of flaws in your regex. For instance, it matches
OST|||| 0.Might I suggest my own P.O. Box regex?
Enable the option to make
^and$match at line breaks (usually,/m), and enable the case insensitive option (usually,/i). Don’t enable the free-spacing option (usually,/x).Here are some examples of strings it matches:
The trailing numbers are captured to backreference 1.