Java学习路线-20:正则表达式

第10 章 : 正则表达式

38 认识正则表达式

JDK >= 1.4
使用正则方便进行数据验证处理,复杂字符串修改

实现字符串转数字


class Demo {
    public static boolean isNumber(String temp){
        char[] chars = temp.toCharArray();
        for(char c : chars){
            if(c > '9' || c < '0'){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String number = "123";
        if(isNumber(number)){
            int i  = Integer.parseInt(number);
            System.out.println(i);
            // 123
        }
    }
}

使用正则表达式

String number = "123";
if(number.matches("\\d+")){
    int i  = Integer.parseInt(number);
    System.out.println(i);
    // 123
}

39 常用正则标记

1、字符匹配

x   任意字符
\\  \
\n  换行
\t  制表符

2、字符集

[abc] 任意一个
[^abc]  不在其中任意一个
[a-zA-Z] 任意字母
[0-9] 一个数字

3、简化字符集

. 任意一个字符
\d 数字[0-9]
\D 等价于[^0-9]
\s 匹配任意空格,换行,制表符
\S 匹配非空格数据
\w 字母、数字、下划线等价于[a-zA-Z_0-9]
\W 非字母、数字、下划线等价于[^a-zA-Z_0-9]

4、边界匹配

^ 匹配开始
$ 匹配结束

5、数量

? 0次或1次
* 0次、1次或多次
+ 1次或多次
{n} 长度=n次
{n,} 长度>=n次
{n,m} 长度>=n and 长度<=m次

6、逻辑表达式,多个正则

XY X之后是Y
X|Y 或
() 整体描述
String str = "123";
String regex = "\\d+";
System.out.println(str.matches(regex));
// true

40 String类对正则的支持

public boolean matches(String regex)
public String replaceFirst(String regex, String replacement) 
public String replaceAll(String regex, String replacement)
public String[] split(String regex)
public String[] split(String regex, int limit) 

示例1:删除非字母和非数字

String str = "asfasdfw3414^&*^&%^&wefwerfdc^&*&*fafdasd";
String regex = "[^a-zA-Z0-9]+";
System.out.println(str.replaceAll(regex, ""));
// asfasdfw3414wefwerfdcfafdasd

示例2:数字分隔拆分字符串

String str = "sdasdf123123ffsadfsda232edasf";
String regex = "\\d+";
String[] list = str.split(regex);
for(String s : list){
    System.out.println(s);
}
/**
 * sdasdf
 * ffsadfsda
 * edasf
 */

示例3:判断字符串是否为数字

String str = "10.1";
String regex = "\\d+(\\.\\d+)?";

if(str.matches(regex)){
    System.out.println(Double.parseDouble(str));
    // 10.1
}

示例4:判断字符串是否为日期

import java.text.ParseException;
import java.text.SimpleDateFormat;

class Demo {

    public static void main(String[] args) throws ParseException {
        String str = "2019-11-17";
        String regex = "\\d{4}-\\d{2}-\\d{2}";

        if (str.matches(regex)) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));
            // Sun Nov 17 00:00:00 CST 2019
        }
    }
}

示例5:判断电话号码
电话号码

51283346      \\d{7,8}
010-51283346  (\\d{3}-)?
(010)51283346 (\(\\d{3}\))?

class Demo {

    public static void main(String[] args) {
        String[] numbers = new String[]{
                "51283346",
                "010-51283346",
                "(010)51283346"
        };

        String regex = "((\\d{3}-)|(\\(\\d{3}\\)))?\\d{7,8}";

        for(String number : numbers){
            System.out.println(number.matches(regex));
        }
        /**
         * true
         * true
         * true
         */
    }
}

示例6:邮箱验证
用户名:数字、字母、下划线(不能开头)
域名:数字、字母、下划线
域名后缀:com、cn、net、com.cn、org

String email = "google@qq.com";
String regex = "[0-9a-zA-Z]\\w+@\\w+\\.(com|cn|net|com.cn|org)";
System.out.println(email.matches(regex));
// true

41 java.util.regex包支持

Pattern 正则表达式编译

private Pattern(String p, int f)
public static Pattern compile(String regex)

Mather 正则匹配

public Matcher matcher(CharSequence input)
public boolean matches()

Pattern示例

import java.text.ParseException;
import java.util.regex.Pattern;

class Demo {

    public static void main(String[] args) throws ParseException {
        String email = "ooxx12ooxx000ooxx";
        Pattern pattern = Pattern.compile("\\d+");
        String[] list = pattern.split(email);
        for (String s : list) {
            System.out.println(s);
        }

        /**
         * ooxx
         * ooxx
         * ooxx
         */
    }
}

Matcher示例

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

class Demo {

    public static void main(String[] args) throws ParseException {
        String number = "6687";
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(number);
        System.out.println(matcher.matches());
        // true

    }
}

拆分,替换,匹配使用String类就可以实现

String不具备的功能:

示例:提取sql中的变量名

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

class Demo {

    public static void main(String[] args) throws ParseException {
        String sql = "insert into student(name, age) values(#{name}, #{value})";

        Pattern pattern = Pattern.compile("#\\{\\w+\\}");
        Matcher matcher = pattern.matcher(sql);

        while (matcher.find()){
            System.out.println(matcher.group(0).replaceAll("#|\\{|\\}", ""));
        }
        /**
         * name
         * value
         */


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值