java.lang.String:How can we replace the list of characters in a String?
String s = "abc$d!efg:i";
s = s.replace('=', ' ');
s = s.replace(':', ' ');
s = s.replace('$', ' ');
s = s.replace('!', ' ');
I want to check a list of illegal characters(Ex: $ = : ! ) present in the String 's' and replace all the occurance with empty space ' '
I feel difficult to follow the above code style.. Because, If list of illegal characters increased, need to add the code again to check & replace the respective illegal character.
Can we refactor the above code with any other simple way?
Is there any other way to use Map/Set in this above example?
> RTFM
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
That document is almost perfectly useless for someone who is just starting to learn regexes. What they need is a good tutorial, and the best one I know of for Java programmers is the one at [url=http://www.regular-expressions.info/]this site[/url]. When you're ready to move on from introductory tutorials, get the latest edition of [url=http://www.amazon.com/exec/obidos/ASIN/0596002890/jgsbookselection]Mastering Regular Expressions[/url], by Jeffrey Friedl.
> > RTFM
> >
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
>
> That document is almost perfectly useless for someone
> who is just starting to learn regexes. What they
> need is a good tutorial, and the best one I know of
> for Java programmers is the one at
> [url=http://www.regular-expressions.info/]this site[/url]. When you're ready to move on from
> introductory tutorials, get the latest edition of
> [url=http://www.amazon.com/exec/obidos/ASIN/0596002890/jgsbookselection]Mastering Regular Expressions[/url], by Jeffrey Friedl.
Dear Uncle,
maybe you want to read that document first before judging its usefulness. And just in case you can't be bothered, the following is an exact quote, which accidently happens to clearly explain the op's question on the usage of \\[\\] in sabre's regex:
Backslashes, escapes, and quoting
The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.
It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct.
Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.