Java 正則表達式:符合以特定字符串開頭 ,以特定字符串結尾的所有結果


    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);
        }
    }

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM