1. 程式人生 > >使用正則替換文件內容中的特殊字元

使用正則替換文件內容中的特殊字元

如果前端使用的是富文字編輯器,那麼在編輯文件的時候資料庫就會存一些樣式標籤例如<br> <p></p> <style>等等,有時間我們需要拿到一些純淨的資料,例如匯出的時間,我們就不想這些標籤展示,那麼就可以使用正則來替換掉這些有一定規律的字元換

 String str1="<p> Hello <style> Word </br>";
        //第一種方法
        Pattern pattern = Pattern.compile("<.*?>");
        Matcher matcher = pattern.matcher(str1);
        String result = matcher.replaceAll("");

        //第二種方法
        String result1 = str1.replaceAll("<.*?>" ,"");

        System.out.println(result);
        System.out.println(result1);

    }

以上兩種方法都可以實現上述需求 輸出為 :Hello Word