1

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"
5
  • 1
    Not sure why you are using lower cases in the regex with the i option rather than directly writing the upper case letters when you are only looking for upper case letters. Commented Jan 13, 2016 at 19:12
  • Regex by itself is just an expression. It does not do anything. You have not made clear how you are going to use it. Commented Jan 13, 2016 at 19:13
  • Not sure what you are going to do with it, but just replacing those letters with blank string can leave you blank spaces on either side of where the directions were, and furthermore, even a comma as in your last example, and won't give you the desired result. Commented Jan 13, 2016 at 19:13
  • I have no idea how you can distinguish a direction from a non-direction letter (like E in apartment 82E) using just a regex and without an AI or any kind of relevant database. Commented Jan 13, 2016 at 19:19
  • @sawa I have updated my question. I am having on requirement where I want to remove the direction from the street address and save it differently. and users inputs can be in any case upper case or lower case so i is used. Commented Jan 16, 2016 at 18:19

3 Answers 3

3

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 boundary

NOTE 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"
Sign up to request clarification or add additional context in comments.

Comments

3
\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

Comments

1
/,?\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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.