简体   繁体   中英

regex to remove characters from the string

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM