Reputation: 701
I'm doing a street name regex and want to match the words:
(\bNORTH|\bSOUTH|\bEAST|\bWEST)
if they occur after the words
(\bSTREET|\bAVENUE)
... plus 10 other street designations (like LANE, DRIVE, etc which I won't include here)
I don't want to match them if they occur before the street designation, because I want to match the entire street name like so:
BOB STREET - Matches "BOB STREET"
BOB STREET WEST - Matches "BOB STREET WEST"
WEST BOB STREET - *** Matches "WEST BOB STREET" ***
NORTH BOB STREET WEST - *** Matches "NORTH BOB STREET WEST" ***
Currently, I have an extremely long regex that looks something like this:
[0-9].*?((\bSTREET (\bWEST|\bEAST|\bNORTH|\bSOUTH)|\bSTREET)|(\bAVENUE (\bWEST|\bEAST|\bNORTH|\bSOUTH)|\bAVENUE))
which gets extremely unwieldy since I have to duplicate WEST, EAST, NORTH, SOUTH once for each of the 15+ street designations.
Is there a way to shorten this to match the street designation and the compass directions - only if they appear after the street designation - without having to duplicate the compass direction regex after each street designation?
Upvotes: 2
Views: 97
Reputation: 2891
If you want a set amount of options followed by a set amount of options that just looks like two OR sets to me. So you should split your options differently. See below how you can avoid copying them every time.
(STREET|AVENUE)[\s](NORTH|SOUTH|EAST|WEST)?
Upvotes: 2