Java正则表达式的完全匹配与部分匹配

时间:2021-12-25 06:11:31
package com.cxtt.test;

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

public class TestZhengze {

public static void main(String[] args) {
// 完全匹配"^[1-9][0-9]{4,}$"
//部分匹配"[1-9][0-9]{4,}"
if (matcherRegularExpression("^[1-9][0-9]{4,}$", "910618858a")) {
System.out.println("输入正确");
}else{
System.out.println("输入错误");
}
}

// 匹配正则表达式方法
public static boolean matcherRegularExpression(String regEx, String str) {
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
boolean found = false;
while (matcher.find()) {
//System.out.println("发现 \"" + matcher.group() + "\" 开始于 "
//+ matcher.start() + " 结束于 " + matcher.end());
found = true;
}
return found;
}

}