按: 这一篇是涉及到Java 处理正则表达式的话题.
代码如下:
package book.javapuzzlers.ch03.p20;
import java.util.regex.Pattern;
public class Me {
public static void main(String[] args) {
String path = Me.class.getName().replaceAll(".", "/");
System.out.println(path + ".class");
path = Me.class.getName().replace(".", "/");
System.out.println(path + ".class");
path = Me.class.getName().replace(Pattern.quote("."), "/");
System.out.println(path + ".class");
path = Me.class.getName().replaceAll(Pattern.quote("."), "/");
System.out.println(path + ".class");
}
}
输出结果是:
/////////////////////////////.class
book/javapuzzlers/ch03/p20/Me.class
book.javapuzzlers.ch03.p20.Me.class
book/javapuzzlers/ch03/p20/Me.class
参考 Java API document, 可以知道 String.replaceAll(CharSequence regex, CharSequence replacement) 的第一个参数是个正则表达式, 于是第一行和第四行的输出结果就找到原因了. 注意, Pattern.quote(".") 即返回 \Q.\E.
对于 String.replace(CharSequence target, CharSequence replacement) 是用 replacement 替换 target, 不使用正则表达式, 第二, 三条打印语句的 replacement 分别是 . 和 \Q.\E, 故得到第二, 三行的输出结果.
附上资料如下:
来自: http://java.sun.com/javase/6/docs/api/java/lang/String.html
public String replace(CharSequence target,
CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
Parameters:
target
- The sequence of char values to be replaced
replacement
- The replacement sequence of char values
Returns:
The resulting string
Throws:
NullPointerException
- if target
or replacement
is null
.
Since:
1.5
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
Pattern
.compile
(regex).matcher
(str).replaceAll
(repl)
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll
. Use Matcher.quoteReplacement(java.lang.String)
to suppress the special meaning of these characters, if desired.
Parameters:
regex
- the regular expression to which this string is to be matched
replacement
- the string to be substituted for each match
Returns:
The resulting String
Throws:
PatternSyntaxException
- if the regular expression's syntax is invalid
Since:
1.4
See Also:
以下来自: http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
public static String quote(String s)
Returns a literal pattern String
for the specified String
.
This method produces a String
that can be used to create a Pattern
that would match the string s
as if it were a literal pattern.
Metacharacters or escape sequences in the input sequence will be given no special meaning.
Parameters:
s
- The string to be literalized
Returns:
A literal string replacement
Since:
1.5
附: