0

我有 EditTextsearchKeyword在段落内搜索(段落在 SQLite 数据库中)。

String Contentstr = paragraph.replaceAll(searchKeyword,"<font color=\"red\">" + searchKeyword + "</font>");
txtV.setText(Html.fromHtml(Contentstr));

上面的代码高亮如下:

当我搜索“Hi hi”(searchKeyword=" Hi hi")时,数据库搜索功能返回任意大小写字母(我的意思是“ hI hi”或“ HI HI”或等等),它会高亮显示“ Hi hi”,但会替换所有其他的“ hI HI”、“ hI hi” 、。 . 等进入 searchKeyword=" Hi hi"。
我不想那样换。
当我搜索“ Hi hi”时,它必须突出显示“ Hi hi”和其他“ hI hi”或“ HI HI”而不将其他替换为“ Hi hi”。

4

1 回答 1

0

如果你想让你的正则表达式不区分大小写,请(?i)在它的开头添加。您也可以使用匹配$indexindex索引来替换匹配部分。

因此,使用这些信息,您可以尝试类似

String Contentstr = paragraph.replaceAll("(?i)"+searchKeyword, 
                       "<font color=\"red\">$0</font>");

此外,如果您searchKeyword包含一些特殊字符,则将其包装起来Pattern.quote()以生成将自动转义这些字符的特殊含义的正则表达式

String Contentstr = paragraph.replaceAll("(?i)"+Pattern.quote(searchKeyword), 
                       "<font color=\"red\">$0</font>");
于 2013-05-24T01:52:16.773 回答