正则表达式的获取功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LeoZuosj/article/details/89295285

正则表达式的获取功能

  • A:正则表达式的获取功能
    • Pattern和Matcher的结合使用
  • B:案例演示
    • 需求:把一个字符串中的手机号码获取出来
package com.heima.regex;

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

public class Demo08_Pattern {
	public static void main(String[] args) {
		String s = "我的手机号码是18511866260,我曾用过18987654321,还用过18812345678";
		String regex = "1[3578]\\d{9}";
		
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(s);
		
		//必须得先find然后才能group,否则会报No match found的错.
		/*boolean b1 = m.find();
		System.out.println(b1);
		System.out.println(m.group());
		
		boolean b2 = m.find();
		System.out.println(b2);
		System.out.println(m.group());*/
		
		while(m.find()){
			System.out.println(m.group());
		}
	}
}

猜你喜欢

转载自blog.csdn.net/LeoZuosj/article/details/89295285