regex - Java regular expression -


i want replace 1 of these chars:

% \ , [ ] # & @ ! ^ 

... empty string ("").

i used code:

string line = "[ybi-173]"; pattern cleanpattern = pattern.compile("%|\\|,|[|]|#|&|@|!|^"); matcher matcher = cleanpattern.matcher(line); line = matcher.replaceall(""); 

but doesn't work.

what miss in regular expression?

there several reasons why solution doesn't work.

several of characters wish match have special meanings in regular expressions, including ^, [, , ]. these must escaped \ character, but, make matters worse, \ must escaped java compiler pass \ through regular expression constructor. so, sum step one, if wish match ] character, java string must "\\]".

but, furthermore, case character classes [], rather alternation operator |. if want match "any of characters a, b, c, looks [abc]. character class [%\,[]#&@!^], but, because of java string escaping rules , special meaning of characters, regex [%\\\\,\\[\\]#&@!\\^].


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -