Reputation: 9095
I have street address and I want to replace the street direction with blank string.
this are the streets E, W, S, N, NE, NW, SE, SW
which I want to replace with blank string
I have tried this regex /((e|w)|(n|s)(e|w)?)/i
to replace the direction, but this replacing all the instance characters present in regex.
I would like convert street address like following -
"123 Main Street S" => "123 Main Street"
"E 123 Main Street" => "123 Main Street"
"SE 123 Main Street" => "123 Main Street"
"123 Main Street, SW, US" => "123 Main Street, US"
"123 Main Street, s, USA" => "123 Main Street, USA"
Upvotes: 1
Views: 313
Reputation: 627488
Here is the best I could come up with:
/[\s,]*\b(?:[EW]|[NS][EW]?)\b/i
The regex matches:
[\s,]*
- zero or more whitespaces and commas\b
- word boundary (leading)(?:[EW]|[NS][EW]?)
- a non-capturing grouping construct that matches:
[EW]
- either E
or W
[NS][EW]?
- either N
or S
optionally (thus these are missing from the first alternative) followed by either E
or W
.\b
- trailing word boundaryNOTE that using character classes (those [...]
thingies) to match single characters is the best practice as they do not cause backtracking (so it is preferred to groups with alternatives that are usually used with sequences of characters).
See IDEONE demo
rx = /[\s,]*\b(?:[EW]|[NS][EW]?)\b/i
puts "123 Main Street S".sub(rx, '').strip # => "123 Main Street"
puts "E 123 Main Street".sub(rx, '').strip # => "123 Main Street"
puts "SE 123 Main Street".sub(rx, '').strip # => "123 Main Street"
puts "123 Main Street, SW, US".sub(rx, '').strip # => "123 Main Street, US"
Upvotes: 3
Reputation: 773
/,?\s?\b((e|w)|(n|s)(e|w)?)\b\s?/i
This would also remove the blank spaces around the directions and preceding comma.
Example: https://regex101.com/r/iW8dZ7/2
Upvotes: 1
Reputation: 67988
\b((e|w)|(n|s)(e|w)?)\b
Should do it for you.\b
is word boundary
.See demo.
https://regex101.com/r/iJ7bT6/8
Upvotes: 3