String target = "FOOBar";
target = target.replaceAll("(?i)foo", "");
System.out.println(target);
Output:
Bar
It's worth mentioning that replaceAll treats the first argument as a regex pattern, which can cause unexpected results. To solve this, also use Pattern.quote as suggested in the comments.
String target = "FOOBar";
target = target.replaceAll("(?i)foo", "");
System.out.println(target);
Output:
Bar
It's worth mentioning that replaceAll treats the first argument as a regex pattern, which can cause unexpected results. To solve this, also use Pattern.quote as suggested in the comments.
Just make it simple without third party libraries:
final String source = "FooBar";
final String target = "Foo";
final String replacement = "";
final String result = Pattern.compile(target, Pattern.LITERAL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(source)
.replaceAll(Matcher.quoteReplacement(replacement));
apex - How do I do a case-insensitive replace all for a string? - Salesforce Stack Exchange
Is Java RegEx case-insensitive? - Stack Overflow
regex - Case Insensitive variable for String replaceAll(,) method Java - Stack Overflow
java - regex replace all ignore case - Stack Overflow
The option to replace case insensitive regex in java is Pattern.CASE_INSENSITIVE, which can also be specified as (?i)
(http://boards.developerforce.com/t5/Apex-Code-Development/Why-Pattern-CASE-INSENSITIVE-not-detectable/td-p/204337)
String srcStr = '{!id} value {!Id} is not {!ID}';
String replaceToken = '(?i)\\{!id\\}';
System.debug('REPLACEMENT ' + srcStr.replaceAll(replaceToken, ''));
System.assertEquals(' value is not ', srcStr.replaceAll(replaceToken, ''));
Just curious can you not convert the string to a particular case?
say
String ex = 'Upper';
ex.toLowerCase();
(or)
ex.toUpperCase()
Examples from the official documentation -
String s1 = 'ThIs iS hArD tO rEaD';
System.assertEquals('this is hard to read',
s1.toLowerCase());
String myString1 = 'abcd';
String myString2 = 'ABCD';
myString1 =
myString1.toUpperCase();
Boolean result =
myString1.equals(myString2);
System.assertEquals(result, true);
You can also match case insensitive regexs and make it more readable by using the Pattern.CASE_INSENSITIVE constant like:
Pattern mypattern = Pattern.compile(MYREGEX, Pattern.CASE_INSENSITIVE);
Matcher mymatcher= mypattern.matcher(mystring);
RegexBuddy is telling me if you want to include it at the beginning, this is the correct syntax:
"(?i)\\b(\\w+)\\b(\\s+\\1)+\\b"
Just add the "case insensitive" switch to the regex:
html.replaceAll("(?i)"+label, "WINDOWS");
Note: If the label could contain characters with special regex significance, eg if label was ".*", but you want the label treated as plain text (ie not a regex), add regex quotes around the label, either
html.replaceAll("(?i)\\Q" + label + "\\E", "WINDOWS");
or
html.replaceAll("(?i)" + Pattern.quote(label), "WINDOWS");
String.replaceAll is equivalent to creating a matcher and calling its replaceAll method so you can do something like this to make it case insensitive:
html = Pattern.compile(label, Pattern.CASE_INSENSITIVE).matcher(html).replaceAll("WINDOWS");
See: String.replaceAll and Pattern.compile JavaDocs
To do case-insensitive search and replace, you can change
outText = inText.replaceAll(word, word.replaceAll(" ", "~"));
into
outText = inText.replaceAll("(?i)" + word, word.replaceAll(" ", "~"));
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
You could convert it all to lowercase before doing the search, or look at a regex modifier Pattern.CASE_INSENSITIVE
You can use replaceAll with (?i) which will do case-insensitive replace.
String str = "Software Industry. software Industry. SOFTWARE INDUSTRY. "+
"software industry. "
str = str.replaceAll("(?i)(software industry)", "\\*$1\\*");
You can use Pattern Matching (Regex)for this.
First extract the phrase using group and store in string or any other data structure. then use Replace function to achieve your output.
you can refer this post : http://www.vogella.com/articles/JavaRegularExpressions/article.html