天天看點

正規表達式-比對關鍵字

import java.util.regex.*;

public class PatternTest {

static Pattern p = Pattern.compile("([^\"]{5})(關鍵字)([^\"]{5})"); //設定比對前後5個字元的關鍵詞句子

public static void main(String[] args) throws Exception {

Matcher m = p.matcher("abcdeft關鍵字hijklmnopq");

if(m.find()){

System.out.println(m.group());

}

}

}