Java 使用正则表达式替换单词
可能重复:Java 使用正则表达式替换单词,java,regex,Java,Regex,可能重复: 我需要将所有出现的索尼爱立信替换为介于两者之间的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(wor
我需要将所有出现的索尼爱立信替换为介于两者之间的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
它应该忽略案例并给出所需的输出。将其更改为
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
Sony~Ericsson is a leading company in mobile.
The company sony~ericsson was found in oct 2001
Pattern.compile("(sony) (ericsson)", Pattern.CASE_INSENSITIVE)
.matcher(str)
.replaceAll("$1~$2")
避免破坏原始资本: 然而,在上面的方法中,你破坏了被替换单词的大小写。这里有一个更好的建议:
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
Sony~Ericsson is a leading company in mobile.
The company sony~ericsson was found in oct 2001
Pattern.compile("(sony) (ericsson)", Pattern.CASE_INSENSITIVE)
.matcher(str)
.replaceAll("$1~$2")
str.replaceAll(regex,repl)
Pattern.compile(regex).matcher(str).replaceAll(repl)
。因此,您可以使用以下命令使matcher不区分大小写:
使用反向引用保留大小写:
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
Pattern.compile("(sony) (ericsson)", Pattern.CASE_INSENSITIVE)
.matcher(str)
.replaceAll("$1~$2")
给出:
索尼~爱立信是移动领域的领先公司。索尼爱立信公司成立于2001年10月
输出:
Sony~Ericsson is a leading company in mobile. The company Sony~ericsson was found in oct 2001
可能重复:@ajoobe:太棒了!我该怎么感谢你伙计?谢谢你约翰的建议这是一个优雅的解决方案!但是,如果
word
变量实际上是一个函数的参数,那么使用它就变得更加困难了。@aioobe谢谢。是的,你是对的;为了使其工作,必须知道要匹配的字符串(“sony Ericson”
),以便可以定义组(“(sony)(ericsson)”
)。如果“sony ericcson”
来自“外部”(例如,函数的外部参数),那么这将很棘手(充其量!)。