1 package regex; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public class regexDemo { 7 public static void main(String[] args) { 8 String str1 = "19900330199012121990111119900220"; 9 String str = "((\\d{4})(\\d{2})(\\d{2}))";10 Pattern p = Pattern.compile(str);11 Matcher m = p.matcher(str1);12 13 while (m.find()) {14 // group分组左括号优先匹配。15 System.out.print("出生日期" + m.group(1));16 System.out.print("....");17 System.out.print("出生年" + m.group(2) + " ");18 System.out.print("出生月" + m.group(3) + " ");19 System.out.print("出生日" + m.group(4) + " ");20 // 返回以前匹配的初始索引。21 System.out.print("start:" + m.start() + " ");22 // 返回匹配后的位置。23 System.out.println("end:" + m.end());24 }25 26 String str2 = "
鱼鱼枫 | 这个选手 | 野路子 |