java正则表达式使用示例
package com.hongyuan.test;
import java.util.regex.matcher;
import java.util.regex.pattern;
public class regextest {
public static void main(string[] args) {
string str="<html><head><title>regex test</title></head><body><p>this is a simle regex test</p></body></html>";
//拆分字符串
string[] splitstr=pattern.compile("[</?|>]").split(str);
for(int i=0;i<splitstr.length;i++){
system.out.print(splitstr[i]+" ");
}
system.out.println();
//判断字符串是否与制定模式匹配
boolean ismatching = pattern.compile("^<(\\w*)>.*</\\1>$").matcher(str).matches();
system.out.println(ismatching);
//替换字符串
string repstr=pattern.compile("<(/?)p>").matcher(str).replaceall("<$1h1>");
system.out.println(repstr);
//提取字符串
matcher m = pattern.compile("<title>(.*)</title>").matcher(str);
while(m.find()){
system.out.println(m.group(1));
}
}
}