简体   繁体   English

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

[英]Java: regex replace string

Consider the following String: 考虑以下字符串:

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

I've got an ArrayList<ArrayList<String>> anaphora with the correct matches and sentence number and ArrayList<String> sentences with the sentences from s . 我有一个具有正确匹配项和句子编号的ArrayList<ArrayList<String>> anaphora ,以及具有s的句子的ArrayList<String> sentences Both look like this: 两者都看起来像这样:

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.

Now the problem arises when trying to replace the substrings. 现在,在尝试替换子字符串时出现了问题。

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));
}

Output will be: 输出将是:

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.

The expected output would be in such a way that 'he' gets replaced with 'Ralph': 预期的输出将以这样的方式将“他”替换为“拉尔夫”:

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

Question How can I fix my regex replace so that ONLY the correct 'he' gets replaced? 问题如何修复正则表达式替换,以便仅替换正确的“他”?

As commented above, you can use a word boundary, for example: 如上所述,您可以使用单词边界,例如:

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"));

prints: 印刷品:

Ralph was walking down the street, Ralph saw Mary and fell in love with her. 拉尔夫走在街上,拉尔夫看见玛丽,爱上了她。

you need to be careful of the blanks. 您需要注意空白。 so your regex should only do the replacement, if the replaced string is a word. 因此,如果被替换的字符串是一个单词,则您的正则表达式仅应进行替换。

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

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