用正则表达式替换单词

时间:2011-04-07 07:17:20

标签: java regex

  

可能重复:
  regex replace all ignore case

我需要用它们之间的tilda替换所有出现的索尼爱立信。这就是我试过的

String outText="";
    String inText="Sony Ericsson is a leading company in mobile. The company sony ericsson was found in oct 2001";
    String word = "sony ericsson";
    outText = inText.replaceAll(word, word.replaceAll(" ", "~"));
    System.out.println(outText);

这是

的输出
Sony Ericsson is a leading company in mobile. The company sony~ericsson was found in oct 2001

但我想要的是

Sony~Ericsson is a leading company in mobile. The company sony~ericsson was found in oct 2001

它应该忽略案例&给出所需的输出。

3 个答案:

答案 0 :(得分:6)

将其更改为

outText = inText.replaceAll("(?i)" + word, word.replaceAll(" ", "~"));

使搜索/替换不区分大小写。

String outText="";
String inText="Sony Ericsson is a leading company in mobile. " +
              "The company sony ericsson was found in oct 2001";
String word = "sony ericsson";
outText = inText.replaceAll("(?i)" + word, word.replaceAll(" ", "~"));
System.out.println(outText);

<强>输出:

sony~ericsson is a leading company in mobile.
The company sony~ericsson was found in oct 2001

避免破坏原始大写:

然而,在上述方法中,你破坏了替换词的大写。这是一个更好的建议:

String inText="Sony Ericsson is a leading company in mobile. " +
              "The company sony ericsson was found in oct 2001";
String word = "sony ericsson";

Pattern p = Pattern.compile(word, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(inText);

StringBuffer sb = new StringBuffer();

while (m.find()) {
  String replacement = m.group().replace(' ', '~');
  m.appendReplacement(sb, Matcher.quoteReplacement(replacement));
}
m.appendTail(sb);

String outText = sb.toString();

System.out.println(outText);

<强>输出:

Sony~Ericsson is a leading company in mobile.
The company sony~ericsson was found in oct 2001

答案 1 :(得分:3)

str.replaceAll(regex, repl) is equal to Pattern.compile(regex).matcher(str).replaceAll(repl)。因此,您可以使用flag

使匹配器不区分大小写
Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(str).replaceAll(repl)

使用反向引用来保留大小写

Pattern.compile("(sony) (ericsson)", Pattern.CASE_INSENSITIVE)
       .matcher(str)
       .replaceAll("$1~$2")

给出:

  

索尼〜爱立信是移动领域的领先企业。索尼〜爱立信公司于2001年10月被发现

答案 2 :(得分:0)

String outText = inText.replaceAll("(?i)(Sony) (Ericsson)", "$1~$2");

<强>输出

Sony~Ericsson is a leading company in mobile. The company Sony~ericsson was found in oct 2001