1. 程式人生 > >正則表達式-將搜索結果高亮

正則表達式-將搜索結果高亮

mage util 小寫 find() 搜索結果 match app asa substring

模擬:

將要查找的字符串中,指定字符替換為高亮的字符:

方法一:

package com.java.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HighLight {
    public static void main(String[] args) {
        String keyword = "a";
        String regexp = keyword + "+";
        String name = "啊abfac是的Aaa愛的的a我要是alkja卡和戶A玉玉aaa很快就asa啊發A阿達ff";
        
        Pattern pattern 
= Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(name); StringBuffer sb = new StringBuffer(); int start = 0; int end = 0; String temp = ""; while(matcher.find()){ start = matcher.start(); sb.append(name.substring(end, start)); temp
= "<i class=\"highlight\">"+matcher.group()+"</i>"; sb.append(temp); end = matcher.end(); } sb.append(name.substring(end)); System.out.println(sb.toString()); } }

返回結果:

<i class="highlight">a</i>
bf<i class="highlight">a</i>c是的<i class="highlight">Aaa</i>愛的的<i class="highlight">a</i>我要是<i class="highlight">a</i>lkj<i class="highlight">a</i>卡和戶<i class="highlight">A</i>玉玉<i class="highlight">aaa</i>很快就<i class="highlight">a</i>s<i class="highlight">a</i>啊發<i class="highlight">A</i>阿達ff

技術分享

方法二:

package com.java.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HighLight2 {
    public static void main(String[] args) {
        String keyword = "a";
        String regexp = keyword;
        String name = "啊abfac是的Aaa愛的的a我要是alkja卡和戶A玉玉aaa很快就asa啊發A阿達ff";
        
        Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(name);
        String resp = matcher.replaceAll("<i class=\"highlight\">"+keyword+"</i>");
        System.out.println(resp);
    }
}

結果:

<i class="highlight">a</i>bf<i class="highlight">a</i>c是的<i class="highlight">a</i><i class="highlight">a</i><i class="highlight">a</i>愛的的<i class="highlight">a</i>我要是<i class="highlight">a</i>lkj<i class="highlight">a</i>卡和戶<i class="highlight">a</i>玉玉<i class="highlight">a</i><i class="highlight">a</i><i class="highlight">a</i>很快就<i class="highlight">a</i>s<i class="highlight">a</i>啊發<i class="highlight">a</i>阿達ff

但是問題是,將name中的大寫的A也替換為小寫了。

方法三:

package com.java.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HighLight3 {
    public static void main(String[] args) {
        String keyword = "a";
        String regexp = keyword + "+";
        String name = "啊abfac是的Aaa愛的的a我要是alkja卡和戶A玉玉aaa很快就asa啊發A阿達ff";
        
        Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(name);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, "<i class=\"highlight\">"+matcher.group()+"</i>");
        }
        matcher.appendTail(sb);
        
        System.out.println(sb.toString());
    }
}

結果:

<i class="highlight">a</i>bf<i class="highlight">a</i>c是的<i class="highlight">Aaa</i>愛的的<i class="highlight">a</i>我要是<i class="highlight">a</i>lkj<i class="highlight">a</i>卡和戶<i class="highlight">A</i>玉玉<i class="highlight">aaa</i>很快就<i class="highlight">a</i>s<i class="highlight">a</i>啊發<i class="highlight">A</i>阿達ff

很nice。又簡單又快捷

--------

正則表達式-將搜索結果高亮