1

I have a ListView and a search_term. I want to highlight (by coloring red) all instances of the search_term in the ListView. This works and is easy.

text = text.replaceAll(search_term, "<font color='red'>$1</font>");

I am now trying to make it work in a case in-sensitive manor. I thought the following was correct but it is not working.

text = text.replaceAll("(?i)" + search_term, "<font color='red'>$1</font>");

SO basically I want to color all instances of the search_term in red, I want to ignore case when matching but not when coloring.

Here are 3 examples. The search_term is "apple" and bold represents the color red.

"The apple is red" -> "The apple is red"

"The Apple is red" -> "The Apple is red"

"The APPLE is red" -> "The APPLE is red"

Kind Regards, Cathal

Cathal Coffey
  • 1,105
  • 2
  • 20
  • 35

2 Answers2

1

The search_term should be in round bracket like () which depicts a group..

So for apple your search_term should be

search_term="(apple)";

This would get captured in $1

OR

You can simply use $0 without using ()

NOTE

$0->is the complete match

$1->first () group

$2->second () group

Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

I don't think (?i) works with String.replaceAll. It should work if you use Pattern and Matcher.

Pattern pattern = Pattern.compile( search_term, Pattern.CASE_INSENSITIVE );
Matcher matcher = pattern.matcher( text );
text = matcher.replaceAll( "\0" );

I'm not 100% sure I have the string in replaceAll right, but that's the general idea.

Khantahr
  • 8,156
  • 4
  • 37
  • 60
  • (?i) does work with String.replaceALL http://stackoverflow.com/questions/5054995/how-to-replace-case-insensitive-literal-substrings-in-java – Cathal Coffey Oct 27 '12 at 19:39