Reputation: 12551
I'm trying to extract a lone # on the end of addresses, but only if not after certain designations.
Here is the regex I am testing:
/(?<!apt|appt|appartment|ste|suite|box)[,.]? ?# ?([0-9]+)$/i
The regex should fail in this case:
123 W Somewhere St Apt # 321
The regex should find a match in this:
123 W Somewhere St # 321
However, it matches both of the above.
I found that if I change [,.]? ?# ?
to simply #
then it works.
However, that means that simply removing a space or adding a period to the address will throw it off.
How do I get my negative look-behind to work and allow my regex to remain flexible in this instance?
Implemented in PHP 5.3.
Upvotes: 1
Views: 100
Reputation: 89547
You can't use a lookbehind in this situation (with optional content after), but you can use this alternative:
/(?!\b(?:apt|appt|appartment|ste|suite|box))\b\w+[,.]? ?# ?\K[0-9]+/i
\K
reset all the match before.
Upvotes: 1