public static Patter compile(String args):通过此方法取得Patter实例,并设置正则
public Matcher matcher(CharSequence input):为Matcher类实例化
public String[] split(CharSequence input):字符串拆分
public String pattern():返回正则表达式
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s1 = "DEF reference values true";
String s2 = "DEF sex values female";
String s3 = "DEF Array array1 values {3,4,5,6,5}";
String s4 = "DEF Var name values Jack";
String[] arr = { s1, s2, s3, s4 };
String pat = "^DEF(Array|Var)?(.*?) values (.*)";
Pattern p = Pattern.compile(pat);
for (int i = 0; i < arr.length; i++) {
Matcher m = p.matcher(arr[i]);
//if(m.matches())是否匹配到
if (m.find()) {
String reg = m.group(2).trim();
String regValue = m.group(3).trim();
System.out.println(reg + "\tvalue: " + regValue);
//替换 m.replaceAll("*");
}
}
}
}
String类里支持正则表达式