[web-010]java正则匹配find和matches的区别

find,在一个字符串寻找出所有符合 正则表达式的字符串,可能有多个结果,可以一一输出。matches,检查这个字符串是否符合给定的正则表达式。

find示例

package com.company;

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

public class Main {
    //第一个字母可以是“+”也可以不是,
//    static final Pattern p_tel = Pattern.compile("\\+?[0-9]\\d+");
    static final Pattern p_tel = Pattern.compile("\\+?\\d+");

    public static void main(String[] args) {
        //判定字符串:第一个字母是"+"号后面全是数字; 全是数字。

        Vector<String> phoneVec = new Vector<String>();
        phoneVec.add("18717917888");
        phoneVec.add("s18717917888");
        phoneVec.add("18p7179178 88");
        phoneVec.add("+118717917888");
        phoneVec.add("+8618717917888");
        phoneVec.add("187 17917888");
        phoneVec.add("187 179 17888");
        phoneVec.add(" 187 179 17888 ");
        phoneVec.add("+ 187 179 17888 ");

        for(String phone: phoneVec){
            System.out.println("\n\n");
            System.out.println("phone=|"+phone+"|");
            if (phone.contains(" ")){
                phone = phone.replace(" ", "");
                System.out.println("phone-new=|"+phone+"|");
            }

            Matcher m = p_tel.matcher(phone);
            while (m.find()){
                System.out.println("find:"+m.group());
            }

        }
    }
}

输出结果如下




phone=|18717917888|
find:18717917888



phone=|s18717917888|
find:18717917888



phone=|18p7179178 88|
phone-new=|18p717917888|
find:18
find:717917888



phone=|+118717917888|
find:+118717917888



phone=|+8618717917888|
find:+8618717917888



phone=|187 17917888|
phone-new=|18717917888|
find:18717917888



phone=|187 179 17888|
phone-new=|18717917888|
find:18717917888



phone=| 187 179 17888 |
phone-new=|18717917888|
find:18717917888



phone=|+ 187 179 17888 |
phone-new=|+18717917888|
find:+18717917888

Process finished with exit code 0

matches示例,java代码如下

package com.company;

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

public class Main {
    //第一个字母可以是“+”也可以不是,
//    static final Pattern p_tel = Pattern.compile("\\+?[0-9]\\d+");
    static final Pattern p_tel = Pattern.compile("\\+?\\d+");

    public static void main(String[] args) {
        //判定字符串:第一个字母是"+"号后面全是数字; 全是数字。

        Vector<String> phoneVec = new Vector<String>();
        phoneVec.add("18717917888");
        phoneVec.add("s18717917888");
        phoneVec.add("18p7179178 88");
        phoneVec.add("+118717917888");
        phoneVec.add("+8618717917888");
        phoneVec.add("187 17917888");
        phoneVec.add("187 179 17888");
        phoneVec.add(" 187 179 17888 ");
        phoneVec.add("+ 187 179 17888 ");

        for(String phone: phoneVec){
            System.out.println("\n\n");
            System.out.println("phone=|"+phone+"|");
            if (phone.contains(" ")){
                phone = phone.replace(" ", "");
                System.out.println("phone-new=|"+phone+"|");
            }

            Matcher m = p_tel.matcher(phone);
            if (m.matches()){
                System.out.println("find:"+m.groupCount()+":"+m.group());
            }
        }
    }
}

结果如下:



phone=|18717917888|
find:0:18717917888



phone=|s18717917888|



phone=|18p7179178 88|
phone-new=|18p717917888|



phone=|+118717917888|
find:0:+118717917888



phone=|+8618717917888|
find:0:+8618717917888



phone=|187 17917888|
phone-new=|18717917888|
find:0:18717917888



phone=|187 179 17888|
phone-new=|18717917888|
find:0:18717917888



phone=| 187 179 17888 |
phone-new=|18717917888|
find:0:18717917888



phone=|+ 187 179 17888 |
phone-new=|+18717917888|
find:0:+18717917888

猜你喜欢

转载自blog.csdn.net/u011539200/article/details/82969072