Java God -- An overall understanding of regex in Java

Copyright © 2019 SheepCore Authentic Articles.

If you wanna reblog the article, please mark the provenience:

https://blog.youkuaiyun.com/qq_38812171/article/details/87292424

1. Main focus:  

    * 正则表达式可以构造复杂的文本模式,并对输入的字符串进行搜索。

    * 正则表达式的实质是一种描述字符串的方式。

    * How to use Java Regex to solve string-related problems.

2.Basic skill:  

  Understanding the rules of constructing a ture regex is the key point to get command of Java regex.

    Example: how to match an integer from a given string?

     整数的一般格式为:-12, 34, +5,0等。对于负数要匹配前面的符号,整数可以有符号也可以没有符号。符号跟有一个或多个整数数字。以下来简单介绍以下几种常用正则表达式的表示符号。

    * ? -- yes or no

     如果要匹配[-9~-1]之间的所有整数,可以用正则表达式  "-?\\d"。

    * + -- one or more

    如果要匹配所有正整数,可以用正则表达式 "\\d+"。

    * \\d -- one digit

    如果要匹配所有整数,可以用正则表达式 "-|\\+?\\d+"

    * \\ -- 两个反斜线才表示反斜线 \,例如 "\\d\\d" 表示45, 56等两位数字的整数。

    * \\w -- 小写w, 表示一个单词字符(a~z或A~Z)。

    * \\W -- 大写W, 表示非单词字符(除了a~z,A~Z,0~9以外的字符)。

3.Examples:

example 1:

// signed integer matching
System.out.println("+112".matches("(-|\\+)?\\d+"));

output:

 true

 

example 2:

// unsigned integer matching
System.out.println("34".matches("\\d+"));

output:

 true

 

example 3:

// decimal matching
System.out.println("3.1415926".matches("-|\\+?\\d+.?\\d+"));

output:

 true

 

example 4:

// scientific decimal matching
System.out.println("-3.14e-2".matches("(-|\\+)?\\d+.?\\d+(e|E)(-|\\+)?\\d+"));

output:

 true

 

example 5:

System.out.println("sheepCore@neu.edu.cn".matches("\\w+@neu.edu.cn"));
//邮箱匹配

output:

 true

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值