繁体   English   中英

Java:正则表达式替换字符串

[英]Java: regex replace string

考虑以下字符串:

s = "Ralph was walking down the street, he saw Mary and fell in love with her. Judy loves her hair."

我有一个具有正确匹配项和句子编号的ArrayList<ArrayList<String>> anaphora ,以及具有s的句子的ArrayList<String> sentences 两者都看起来像这样:

anaphora.get(0) = [0, Ralph, he]
anaphora.get(1) = [0, Mary, her]
anaphora.get(2) = [0, the street]
anaphora.get(3) = [1, Judy, her]
anaphora.get(4) = [1, her hair]

sentences.get(0) = Ralph was walking down the street, he saw Mary and fell in love with her.
sentences.get(1) = Judy loves her hair.

现在,在尝试替换子字符串时出现了问题。

sentence = sentences.get(0);
if (anaphora.get(0).size()>2){
    example1 = sentence.replaceAll("[^a-zA-Z]"+anaphora.get(0).get(i)+"[^a-zA-Z]", anaphora.get(0).get(1));
    example2 = sentence.replaceAll(anaphora.get(0).get(i), anaphora.get(0).get(1));
}

输出将是:

example1 = Ralph was walking down the street,Ralphsaw Mary and fell in love with her.
example2 = Ralph was walking down tRalph street, Ralph saw Mary and fell in love with Ralphr.

预期的输出将以这样的方式将“他”替换为“拉尔夫”:

Ralph was walking down the street, Ralph saw Mary and fell in love with her.

问题如何修复正则表达式替换,以便仅替换正确的“他”?

如上所述,您可以使用单词边界,例如:

String s = "Ralph was walking down the street, he saw Mary and fell in love with her.";
System.out.println(s.replaceAll("\\bhe\\b", "Ralph"));

印刷品:

拉尔夫走在街上,拉尔夫看见玛丽,爱上了她。

您需要注意空白。 因此,如果被替换的字符串是一个单词,则您的正则表达式仅应进行替换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM