1

I'm trying to parse certain addresses, and I'd like to remove out the special characters except the / in s/o | S/O | d/o | D/O | w/O etc.

So, lets say I have a string. Something like.

@Jane Doe.
W/O John Doe.
House #250, 
Campbell & Jack ^Street,
\Random * State,  Nowhere.

What sort of regex would I use in

String parsedString = addressString.replaceAll(regex,"");

so that parsedString would have the output.

Jane Doe
W/O John Doe
House 250
Campbell Jack Street
Random  State,  Nowhere

(I'm replacing @ , . # ^ & / (except in W/O) )

Cœur
  • 34,719
  • 24
  • 185
  • 251
The 0bserver
  • 736
  • 5
  • 18

1 Answers1

3

You can use this pattern with the option case insensitive:

String pat = "[@#&^*/.,](?i)(?<![wds]/(?=o))";

details:

[@#&^*/.,]  # characters you want to remove
(?i)       # switch the case insensitive flag
(?<!       # negative lookbehind: not preceded by
    [wds]/ # w or d or s and a slash
    (?=o)  # lookahead: followed by a o
)
Casimir et Hippolyte
  • 85,718
  • 5
  • 90
  • 121