Topic: 关于字符串替换的一个怪现象

  Print this page

1.关于字符串替换的一个怪现象 Copy to clipboard
Posted by: snowbird2005
Posted on: 2005-06-09 23:06

public class ReplaceStr
{

public static void main(String[] args)
{
String str="awefda$asd$;szdjfgv$asd$asdfa";
System.out.println(str);
String newString=str.replaceAll("$asd$","hello");
System.out.println(newString);
}

}
在以上程序中,用hello替换字符串$asd$,但是结果未变,不能替换,即newString仍然和str一样;
但如果将要替换的字符中的$改成其它字符如%, 即被替换字符为%asd%,则可以正常替换,所有%asd%变成hello.
当然不止%可以,一般字符如a,v等都行。但只要被替换字符中有$就不能替换。哪位达人帮忙解释一下?

2.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: why
Posted on: 2005-06-09 23:12

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
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)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
Boundary matchers
$ The end of a line

Therefore, you have to esacpe for $, i.e. \$

3.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: snowbird2005
Posted on: 2005-06-09 23:54

$ The end of a line

你的意思是$ 表示一行的结尾?
不好意思,我的基础比较差。能不能详细说一下?
如果这样,还有那些字符要避免使用?

4.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: why
Posted on: 2005-06-10 00:04

對,$ 用來搜一行的结尾
^.?*()[]\|可能要使用escape,不是避免使用
請看文檔吧
敝人認為自己不會說得比文檔淸晰詳細,更沒耐心做這種水磨工夫

5.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: snowbird2005
Posted on: 2005-06-10 00:31

使用escape? 你是说用转义符\
我看了下文档,英文太差,看不太懂;
那么我的程序怎么改就可以?

6.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: why
Posted on: 2005-06-10 00:56

snowbird2005 wrote:
使用escape? 你是说用转义符\
我看了下文档,英文太差,看不太懂;
那么我的程序怎么改就可以?

Yes,
String newString=str.replaceAll("\\$asd\\$","hello");

correction: ^.?*()[]\|可能要使用escape

7.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: mesocool
Posted on: 2005-06-10 01:13

public final static String DOLLARSIGN = "\u0024";

8.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: snowbird2005
Posted on: 2005-06-10 19:59

谢谢why的多次回复。
我刚试了一下,可以替换。
有一点还是没明白,为什么引号"可以用一个斜杠\转义(\"),而$必须用两个斜杠转义(\\$)

另外,楼上mesocool的意思是不是可以用unicode编码代替$,但我直接代替不行,
String newString=str.replaceAll("\u0024asd\u0024","hello");
该怎么在这里使用unicode编码呢?

9.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: why
Posted on: 2005-06-10 21:08

snowbird2005 wrote:
有一点还是没明白,为什么引号"可以用一个斜杠\转义(\"),而$必须用两个斜杠转义(\\$)

另外,楼上mesocool的意思是不是可以用unicode编码代替$,但我直接代替不行,
String newString=str.replaceAll("\u0024asd\u0024","hello");
该怎么在这里使用unicode编码呢?


"\\$asd\\$" ==> \$asd\$
"\$asd\$" ==> (what \$ is)asd(what \$ is)

I don't catch what mesocool was susggesting. But your statement should be:
String newString=str.replaceAll("\\u0024asd\\u0024","hello");

10.Re:关于字符串替换的一个怪现象 [Re: snowbird2005] Copy to clipboard
Posted by: snowbird2005
Posted on: 2005-06-10 22:08

String newString=str.replaceAll("\\u0024asd\\u0024","hello");
通过你建议的上面的代码可以成功替代。

以下英文摘自JAVA API帮助文件,参照你的解释,我觉得有点明白它的意思了。
主要就是说:在字符串中要想转义必须使用双反斜杠,否则就是另外一个意思了。谢谢你的解释,非常感谢!
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.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923