java--正则表达式

正则表达式

正则表达式(Regular Expression ,在代码中常简写成regex、regexp或RE)是从jdk1.4引入到java中的。

正则标记

所有正则表达式支持的类都定义在java.util.regex包里。

主要由两个类:

  • Pattern类:主要定义要使用的表达式对象。
  • Matcher类:用于进行正则标记与指定内容的匹配操作。

所有使用的正则标记都在 java.util.regex.Pattern类的说明文档中定义,常用的有:

  • 单个字符(数量:1)
    • 字符:表示由一位字符组成;
    • \ \ : 表示准仪字符"\";
    • “\t” : 表示一个"\t"符号;
    • “\n” : 匹配换行(\n)符号;
  • 字符集(数量:1)
    • [ abc ] : 表示可能是字符 a 、字符 b 、字符 c中的任意一位;
    • [ ^abc ] : 表示不是字符 a 、b 、c中的任意一位;
    • [ a-z ] : 表示所有的小写字母;
    • [ a-zA-Z ] : 表示任意一位字母,不区分大小写;
    • [ 0-9 ] : 表示任意的一位数字;
  • 简化的字符集表达式(数量:1)
    • . : 表示任意一位字符;
    • \d : 等价于 “[ 0-9 ]”;
    • \D : 等价于 “[ ^0-9 ]”;
    • \s : 表示任意的空白字符,例如:"\t","\n";
    • \S : 表示任意的非空白字符;
    • \w : 等价于"[ a-zA-Z_0-9 ]",表示由任意的字母、数字、_ (下划线)组成;
    • \W : 等价于"[ ^ a-zA-Z_0-9 ]",表示不是由任意的字母、数字、_ (下划线)组成;
  • 边界匹配:
    • ^ : 正则的开始;
    • $ : 正则的结束;
  • 数量表达:
    • 正则? : 表示此正则可以出现 0 次或1次;
    • 正则+ : 表示此正则可以出现1次或1次以上;
    • 正则* : 表示可以出现0次、1次或者多次;
    • 正则{n} : 表示侧正则正好出现 n 次;
    • 正则{n, } : 表示侧正则出现 n 次以上;
    • 正则{ n,m } : 表示侧正则出现 n~m次;
  • 逻辑运算:
    • 正则1 正则2 :正则1 判断完成后继续判断正则2;
    • 正则1 | 正则2:正则1 或者正则2 有一组满足即可;
    • ( 正则 ) :将多个正则作为一组,可以为这一组单独设置出现的次数;

String 类对正则的支持

虽然 Java 本身提供的正则支持类都在 java.util.regex 包中,但是从实际的使用来讲,只有很少情况才会利用这个包中的Pattern 或 Matcher 类操作正则,大部分情况下都会考虑使用 java.lang.String类中提供的方法来直接简化正则的操作。

String类与正则有关的5个操作方法:

方法名描述
public boolean matches(String regex)正则验证,使用指定的字符串判断其是否符合给出的正则表达式结构
public String replaceAll(String regex,String replacement)将满足正则标记的内容全部替换为新的内容
public String replaceFirst(String regex,String replacement)将满足正则标记的首个内容替换为新的内容
public String[] split(String regex)按照指定的正则标记进行字符串的全拆分
public String[] split(String regex,int limit)按照指定的正则标记进行字符串的部分拆分

例子:实现字符串的替换

public class TestDemo {
    public static void main(String[] args) {
        String string = "hello^#&*^%#@(*7659word6546545723415*-/-+9";
        String regex = "[^a-z]";
        String replaceAll = string.replaceAll(regex, "");
        System.out.println(replaceAll);
    }
}

执行结果:

helloword

字符串的拆分:

public class TestDemo {
    public static void main(String[] args) {
        String string = "hello1354341354world13554!";
        String regex = "\\d+"; 
        String[] split = string.split(regex);
        for (String s : split) {
            System.out.println(s);
        }
    }
}

执行结果:

hello
world
!

为什么使用 “\\d”?

因为要将""转义。

扩展:小数的验证

public class TestDemo {
    public static void main(String[] args) {
        String string = "10.10";
        String regex = "\\d+(\\.\\d+)?";
        if (string.matches(regex)){
            double parseDouble = Double.parseDouble(string);
            System.out.println(parseDouble);
        }
    }
}

执行结果:

10.1

分析:因为 "."可以表示任意字符,所以需要转义成普通的小数点。

IP的验证:

public class TestDemo {
    public static void main(String[] args) {
        String string = "192.168.1.1";
        String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
        if (string.matches(regex)){
            System.out.println(string);
        }
    }
}

执行结果:

192.168.1.1

日期的验证:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDemo {
    public static void main(String[] args) throws ParseException {
        String string = "2018-12-18";
        String regex = "\\d{4}-\\d{1,2}-\\d{1,2}";
        if (string.matches(regex)){
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(string);
            System.out.println(date);
        }
    }
}

Email的验证:

public class TestDemo {
    public static void main(String[] args) {
        String string = "wangyiyouxiang@163.com";
        String regex = "\\w+@\\w+\\.\\w+";
        if (string.matches(regex)){
            System.out.println(string);
        }
    }
}

更严格一点:

public class TestDemo {
    public static void main(String[] args) {
        String string = "wangyiyouxiang@163.com";
        String regex = "\\w+@\\w+\\.(com|net|cn|com\\.cn|net\\.cn|org|gov|edu)";
        if (string.matches(regex)){
            System.out.println(string);
        }
    }
}

Pattern和Matcher

Pattern类的常用方法:

方法描述
public static Pattern complie(String regex)编译正则表达式
public String[] split(CharSequence input)数据全拆分操作
public String[] split(CharSequence input,int limit)数据的部分拆分
public Matcher matcher(CharSequence input)取得Matcher类对象

例如:

import java.util.Arrays;
import java.util.regex.Pattern;

public class PatternDemo {
    public static void main(String[] args) {
        String string = "hello^#&*^%#@(*7659word6546545723415*-/-+9";
        String regex = "[^a-z]+";
        Pattern compile = Pattern.compile(regex);
        String[] split = compile.split(string);
        System.out.println(Arrays.toString(split));
    }
}

执行结果:

[hello, word]

Matcher类的常用方法:

方法描述
public boolean matches()正则匹配
public String replaceAll(String replacement)全部替换
public String replaceFirst(String replacement)替换首个

例如:

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

public class MatcherDemo {
    public static void main(String[] args) {
        String string = "100";
        String regex = "\\d+";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(string);
        boolean matches = matcher.matches();
        System.out.println(matches);
    }
}

执行结果:

true
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值