码迷,mamicode.com
首页 > 编程语言 > 详细

Java中正则表达式以及Pattern和Matcher类

时间:2019-05-13 14:42:15      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:a*   sha   注意   字符串   split()   返回   ace   arrays   imp   

正则表达式
  正则表达式就是正确规则的表达式,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串,就是一种规则的应用。

规则字符在java.util.regex Pattern类中
字符

x 字符 x。举例:‘a’表示字符a
\ 反斜线字符。
\n 新行(换行)符 (’\u000A’)
\r 回车符 (’\u000D’)
字符类

[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括
预定义字符类
任何字符用 \表示
.\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
边界匹配器

^ 行的开头
$ 行的结尾
\b 单词边界(就是不是单词的地方)
Greedy 数量词

X? X,一次或一次也没有 比如""空串 就是没有
X* X,零次或多次 大于等于1次 都算多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,大于等于n 次并且小于等于 m 次

public class Mytest6 {
    public static void main(String[] args) {
        String regx = "a";//用来匹配是否是字母"a"
               regx = "[a,d,r,s,v,h]";//允许出现里面的某一个字母
               regx = "[a-z]";//允许出现所有小写字母里面的某一个字母
               regx = "[0-9]";//允许出现0-9数字中的某一个
               regx = "[A-Za-z]";
               regx = "[a-zA-Z0-9]";
               regx = "[^0-9]";//允许出现不在0-9中的任意一个
               regx = ".";//匹配任意单个字符
               regx = "\\.";//  \\为转义字符 \\.就是匹配我这个点本身
               regx = "\\|";
               regx = "\\d";//等价于[0-9]
               regx = "\\w";//等价于[a-zA-Z0-9_]注意多了一下划线
               regx = "[a-z]+";//允许出现列表中的一个或者多个
               regx = "q?";//允许出现一次或者0次,空串就是没有
               regx = "[a-z]*";//零次或者多次(>=1)次
               regx = "[a-z]{6}";//正好六次不能多也不能少
               regx = "[a-z]{6,}";//至少6次
               regx = "[a-z]{4,27}";//大于等于4次或者小于等于27次
        boolean matches = "sjisdjk".matches(regx);
        System.out.println(matches);
    }
}

正则表达式的分割功能

  • split()方法
  • String类的功能:public String[] split(String regex),其返回的是一个字符串数组。

使用方法可以从下面的代码中看出

 

import java.util.Arrays;

public class Mytest7 {
    public static void main(String[] args) {
        //我有如下一个字符串:”91 27 46 38 50”,
        // 输出是:”27 38 46 50 91”
        String str = "91 27 46 38 50";
        String[] s = str.split(" ");
        int []arr = new int[s.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(s[i]);
        }
        Arrays.sort(arr);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]).append(" ");
        }
        String s1 = sb.toString().trim();
        System.out.println(s1);
    }
}

  

正则表达式的替换功能

String类的功能:public String replaceAll(String regex,String replacement)
代码看一下

public class Mytest8 {
    public static void main(String[] args) {
        String str = "zhang3247893xue345342yous5543hiyigehaoren";
       //使用传统的方法replace()就很麻烦
        String s1 = str.replaceAll("[0-9]", "");
        System.out.println(s1);
    }
}

运行结果为:

zhangxueyoushiyigehaoren

 

正则表达式的获取功能
Pattern和Matcher的结合使用
模式器里面可以封装一个正则表达式
先使用find()方法先找到 再使用group()方法获取出来

Pattern类 正则表达式的编译表现形式。
指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
Matcher类 通过解释 Pattern 对 character sequence 执行匹配操作的引擎。
通过调用模式的 matcher 方法从模式创建匹配器。创建匹配器后,可以使用它执行三种不同的匹配操作:

matches 方法尝试将整个输入序列与该模式匹配。
lookingAt 尝试将输入序列从头开始与该模式匹配。
find 方法扫描输入序列以查找与该模式匹配的下一个子序列。
模式器与匹配器的典型调用顺序为:
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaaab”);
boolean b = m.matches();

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

public class Mytest9 {
    public static void main(String[] args) {
        //获取下面这个字符串中由三个字符组成的单词
        String str="da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
        String regx="\\b[a-z]{3}\\b";
        //获取模式器
        Pattern m = Pattern.compile(regx);
        //获取匹配器
        Matcher p = m.matcher(str);
        while (p.find()){
            String s = p.group();
            System.out.println(s);
        }

    }
}

  

import java.util.Scanner;

public class Mytest4 {
    public static void main(String[] args) {
        //需求:校验qq号码.
        //1:要求必须是5 - 15 位数字
        //2:0 不能开头
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的qq号码");
        String s = sc.nextLine();
        boolean b = checkqqNum(s);
        if(b){
            System.out.println("输入正确");
        }else{
            System.out.println("你输入的规则不正确");
        }
    }
    
    private static boolean checkqqNum(String s) {
        String regx = "[1-9][0-9]{4,14}";
        boolean matches = s.matches(regx);
        return matches;
    }
}

  

import java.util.Scanner;

public class Mytest5 {
    public static void main(String[] args) {
        //手机号码,长度11位  以 1开头
        //手机号码每一位都是数字
        // 第二位 3 5 6 7 8 9
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入的电话号码");
        String s = sc.nextLine();
        boolean b = checkPhoneNum(s);
        if(b){
            System.out.println("你输入的格式正确");
        }else{
            System.out.println("你输入的格式错误");
        }
    }

    private static boolean checkPhoneNum(String s) {
        String regx = "[1][3,5,6,7,8,9][0-9]{9}";
        boolean matches = s.matches(regx);
        return matches;
    }
}

  

原文:https://blog.csdn.net/xupt_rl/article/details/89818804

Java中正则表达式以及Pattern和Matcher类

标签:a*   sha   注意   字符串   split()   返回   ace   arrays   imp   

原文地址:https://www.cnblogs.com/qbdj/p/10856081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!