标签:imp rip art 结束 表达 类型 否则 ring pat
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @ClassName PatternMatchExample * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/4/14. */ public class PatternMatchExample { public static void main(String[] args) { //匹配手机号的正则示例 Pattern pattern = Pattern.compile("1[34785]\\d{9}"); String string = "a的电话号是13212312123,b的电话是13332141234"; Matcher matcher = pattern.matcher(string); // System.out.println(matcher.find()); //find找是否有匹配的子串 // System.out.println(matcher.group()); //必须find()后才能group找到,或者整个字符串完全匹配才行 while (matcher.find()){ System.out.println(matcher.group() + " 匹配的起始下标:"+ matcher.start() + " 匹配的结束下标:" + matcher.end()); } System.out.println(matcher.matches()); //false整个字符串完全匹配才行 System.out.println(matcher.lookingAt()); //false 从字符串开头开始匹配 Pattern pattern1 = Pattern.compile(".{6}1[34785]\\d{9}.*"); Matcher matcher1 = pattern1.matcher(string); System.out.println(matcher1.matches()); //true 整个字符串完全匹配 } }
java 数据类型String 【正则表达式】匹配工具 Pattern和Matcher
标签:imp rip art 结束 表达 类型 否则 ring pat
原文地址:https://www.cnblogs.com/zhangmingda/p/14660358.html