public static void main(String[] args) {
// 查找的字符串
String str = "select t.id,t.name from urrp_db.imas_xmhk_ll t left join urrp_db.imas_ckbx_bl a on t.id=a.id";
//Java正則表達式以括號分組,第一個括號表示以"IMAS_"開頭,第三個括號表示以" "(空格)結尾,中間括號為目標值,
Pattern p = Pattern.compile("(IMAS_)(.*?)( )");
Matcher m = p.matcher(str.toUpperCase());
while(m.find()){
// 取出目標值
String matchStr = m.group(2);
System.out.println("匹配內容:"+matchStr);
str = str.toUpperCase().replace(matchStr,matchStr+"_HL");
System.out.println(str);
}
}
運行結果: