Java正則解析HTML一例

rgqancy發表於2016-03-03

 

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

static String teststr = "UAPPROJECT_ID='402894cb4833decf014833e04fd70002 ; \n\r */' select ";

/**
* 包含回車換行符的處理
*/
public static 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 static void testb(){
String result = teststr.replaceAll("(?s)'.*?'", "");
System.out.println("result:" + result);
}

public static void testc() {
String a = "abc \r\n您的查詢碼為:20160302175452496865,請妥善儲存!\r\n def";
String b = a.replaceAll("(?s).*您的查詢碼為:", "").replaceAll("(?s),請妥善儲存!.*", "");
System.out.println("result:" + b);
}

public static void main(String[] args) {
testa();
testb();
testc();
}
}

相關文章