Looking to validate PO Box but wanted to know if such validation existed. I have the Address field split into Address 1 and Address 2 (Where such PO, Apt, Suite info would go)
Let's go through it...
/ # Beginning of the regex
^ # Beginning of the string
\s* # (Any whitespace)
((
P # Matches your P
(OST)? # Matches your ost
.? # Matches the space
\s* # (Any whitespace)
O # Expects an O - you don't have one. Regex failed.
As of now with your regex, the 'O' in 'OFFICE' is required. Try ^\s*((P(OST)?.?\s*(O(FF(ICE)?))?.?\s+(B(IN|OX))?)|B(IN|OX))
instead (grouping the 'O' in a conditional match).
EDIT: That should be /^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i
instead. BTW, http://rubular.com/ is a pretty good regular expression testing engine. Always nice to know of new tools :)
This one works better, as it removes the unneeded groups in the match set and just returns the whole match.
Skips Post 123:
/^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)+)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i
Doesn't Skip Post 123:
/^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)?)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i
Remove the \d+ at the end to skip the number requirement.