replace words using regex [duplicate]
Possible Duplicate:
regex replace all ignore case
I need to replace all occurrences of Sony Ericsson with a tilda in between them. This is what I have tried
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);
The output of this is
Sony Ericsson is a leading company in mobile. The company sony~ericsson was found in oct 2001
But wh开发者_JS百科at I want is
Sony~Ericsson is a leading company in mobile. The company sony~ericsson was found in oct 2001
It should ignore cases & give the desired output.
Change it to
outText = inText.replaceAll("(?i)" + word, word.replaceAll(" ", "~"));
to make the search / replace case insensitive.
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);
Output:
sony~ericsson is a leading company in mobile.
The company sony~ericsson was found in oct 2001
Avoid ruining the original capitalization:
In the above approach however, you're ruining the capitalization of the replaced word. Here is a better suggestion:
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);
Output:
Sony~Ericsson is a leading company in mobile.
The company sony~ericsson was found in oct 2001
str.replaceAll(regex, repl)
is equal to Pattern.compile(regex).matcher(str).replaceAll(repl)
. Thus, you can make your matcher case-insensitive with a flag:
Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(str).replaceAll(repl)
Using backreferences to preserve case:
Pattern.compile("(sony) (ericsson)", Pattern.CASE_INSENSITIVE)
.matcher(str)
.replaceAll("$1~$2")
Gives:
Sony~Ericsson is a leading company in mobile. The company sony~ericsson was found in oct 2001
String outText = inText.replaceAll("(?i)(Sony) (Ericsson)", "$1~$2");
Output:
Sony~Ericsson is a leading company in mobile. The company Sony~ericsson was found in oct 2001
精彩评论