Java的正则表达式使用,说明在代码中,参照了网络上的一些资料,在此记录下,方便自己如果可能也方便下别人。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Regex Pattern Matcher
* @author 我是谁
*
*/
public class RegexPatternMatcherTest {
public static void main(String[] args) {
complexRegularTest();
find();
// lookingAt();
// matches();
// find2();
// regex();
// groupTest();
}
/**
* 复杂正则测试
*/
public static void complexRegularTest(){
String PATTERN;
PATTERN = "^(/(WEB-INF|special)/)(hello/)([0-9a-zA-Z_]+/){1}(reciept/)$";//合法
Pattern pattern = Pattern.compile(PATTERN);
String jspPath = "/WEB-INF/hello/_default_/reciept/";
Matcher matcher = pattern.matcher(jspPath);
System.out.println(Pattern.matches(PATTERN,jspPath));
if (!matcher.matches()) {
System.out.println("1很抱歉,你输入的URL不符合要求。");
}
System.out.println("1测试完毕");
jspPath = "/WEB-INF/hello/ydybbxba/reciept/";
matcher = pattern.matcher(jspPath);
if (!matcher.matches()) {
System.out.println("2很抱歉,你输入的URL不符合要求。");
}
System.out.println("2测试完毕");
jspPath = "/WEB-INF/classes/ydybbxba/reciept/";
matcher = pattern.matcher(jspPath);
if (!matcher.matches()) {
System.out.println("3很抱歉,你输入的URL不符合要求。");
}
System.out.println("3测试完毕");
jspPath = "/WEB-INF/hello/ydybbxba/reciept2/";
matcher = pattern.matcher(jspPath);
if (!matcher.matches()) {
System.out.println("4很抱歉,你输入的URL不符合要求。");
}
System.out.println("4测试完毕");
jspPath = "/WEB-INF/hello/ydybbxba/reciept/alter.class";
matcher = pattern.matcher(jspPath);
if (!matcher.matches()) {
System.out.println("5很抱歉,你输入的URL不符合要求。");
}
System.out.println("5测试完毕");
jspPath = "/special/hello/ydybbxba/reciept/";
matcher = pattern.matcher(jspPath);
if (!matcher.matches()) {
System.out.println("6很抱歉,你输入的URL不符合要求。");
}
System.out.println("6测试完毕");
}
//find()对字符串进行匹配,匹配到的字符串可以在任何位置
public static void find(){
System.out.println("---------------------");
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("aaa2223bb");
System.out.println(m.find());//匹配2223
System.out.println(m.start());//返回3
System.out.println(m.end());//返回7,返回的是2223后的索引号
System.out.println(m.group());//返回2223
m=p.matcher("aaa2223bb555");
while(m.find()){
System.out.println(m.start());//1、2轮分别返回3、9
System.out.println(m.end());//1、2轮分别返回7、12
System.out.println(m.group());//1、2轮分别返回2223、555
}
}
//lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
public static void lookingAt(){
System.out.println("---------------------");
Pattern p=Pattern.compile("\\d+");
Matcher m2=p.matcher("2223bb");
System.out.println(m2.lookingAt()); //匹配整个字符串
System.out.println(m2.start()); //返回0
System.out.println(m2.end()); //返回4
System.out.println(m2.group()); //返回2223
}
//matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true
public static void matches(){
System.out.println("---------------------");
Pattern p=Pattern.compile("\\d+");
Matcher m3=p.matcher("2223bb");
System.out.println(m3.matches()); //false,不匹配整个字符串
System.out.println(m3.start()); //抛异常,因没有匹配
System.out.println(m3.end()); //抛异常,因没有匹配
System.out.println(m3.group()); //抛异常,因没有匹配
}
public static void find2(){
Pattern p=Pattern.compile("([a-z]+)(\\d+)");
Matcher m=p.matcher("aaa2223bb");
System.out.println(m.find()); //匹配aaa2223
System.out.println(m.groupCount()); //返回2,因为有2组
System.out.println(m.start(1)); //返回0 返回第一组匹配到的子字符串在字符串中的索引号
System.out.println(m.start(2)); //返回3
System.out.println(m.end(1)); //返回3 返回第一组匹配到的子字符串的最后一个字符在字符串中的索引位置.
System.out.println(m.end(2)); //返回7
System.out.println(m.group(1)); //返回aaa,返回第一组匹配到的子字符串
System.out.println(m.group(2)); //返回2223,返回第二组匹配到的子字符串
}
public static void regex(){
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("我的QQ是:456456 我的电话是:0532214 我的邮箱是:aaa123@aaa.com");
while(m.find()) {
System.out.println("group:"+m.group());
System.out.println("group start:"+m.start());
System.out.println("group end:"+m.end());
}
}
public static void groupTest(){
String text = "abaaabccab1234ababcccab432dog";
String regexp = "(a*b)([0-9]+)";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(text);
while (matcher.find()){
System.out.println("----------------");
System.out.println(matcher.group());
System.out.println(matcher.group(0)); //匹配到的整个字符串
System.out.println(matcher.group(1)); //第一个括号
System.out.println(matcher.group(2)); //第二个括号
}
}
}