更新時間:2021-04-26 12:28:49 來源:動力節(jié)點 瀏覽2204次
默認情況下.*中的.只能匹配出\n以外的字符,如果遇到要匹配的字符串包含回車換行符(多行),則正則表達式遇到換行符后會停止,導(dǎo)致包含回車換行符的串不能正確匹配,解決的辦法是:

設(shè)置Pattern模式為:Pattern.DOTALL
正則表達式寫法:
String reg = "(?s)'.*'";
下面是一個包含回車換行字符的正則表達式替換處理例子。
static String teststr = "UAPPROJECT_ID='402894cb4833decf014833e04fd70002 ; \n\r */' select ";
/**
* 包含回車換行符的處理
*/
public void testa(){
Pattern wp = Pattern.compile("'.*?'", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = wp.matcher(teststr);
String result = m.replaceAll("");
System.out.println("result:" + result);
}
/**
* 包含回車換行符的處理
*/
public void testb(){
String result = teststr.replaceAll("(?s)'.*?'", "");
System.out.println("result:" + result);
}
以上就是動力節(jié)點小編介紹的“Java匹配正則表達式多行的方法”的內(nèi)容,希望對大家有幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為您服務(wù)。