The 0bserver
The 0bserver

Reputation: 986

replaceAll regex excluding certain occurrences

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) )

Upvotes: 1

Views: 89

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89565

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
)

Upvotes: 3

Related Questions