Java 正则表达式
定义
用于检测、测试字符串规则的表达式.
经常用于检测字符串是否符合特定的规则,在网站上经
常用于检测用户输入数据是否符合规范:
- 邮箱
- 电话号码
- 用户名
- 等
matches
boolean matchs(正则) 检测当前字符串是否符合正则规则,不需要直接调包
返回值类型是布尔(boolean)类型
举一个最简单的用HelloWorld的事例
public class Helloworld {
public static void main(String[] args) {
String rule = "Helloworld1";//需要被匹配的String
String a = "Helloworld1";
String b = "Noway2";
//用正则表达式检查是不是两个内容一样
boolean check1 = a.matches(rule);
boolean check2 = b.matches(rule);
System.out.println(check1);//true
System.out.println(check2);//false
}
}
如果我们仅仅想查它是否都是英文,我们可以用
规则 | 复杂正则表达式 | 简单正则表达式 |
---|---|---|
匹配0到9的一个字符 | [0123456789] | [0-9] |
匹配a到z的一个小写字母 | [abcdefghijklmnopqrstuvwxyz] | [a-z] |
匹配A到Z的一个大写字母 | [ABCDEFGHIJKLMNOPQRSTUVWSYZ] | [A-Z] |
上面那个例子变形
public static void main(String[] args) {
String rule = "H[a-z]{9}[1-7]";
String a = "Helloworld1";
String b = "Noway2";
//用正则表达式检查是不是两个内容一样
boolean check1 = a.matches(rule);
boolean check2 = b.matches(rule);
System.out.println(check1);//true
System.out.println(check2);//false
}
这里可能有同学不了解{9}是什么意思,那我们就详细讲讲{ }中可以写什么
{start,end}
语法 | 解释 |
---|---|
{n} | 规定出现n次 |
{n,m} | 规定出现最少n次, 最多m次 |
{n,} | 规定出现最少n次,最多无限次 |
所以上面{9}的意思是字母出现9次。
还有更简便的写法
语法 | 解释 |
---|---|
X? | 相当于X{0,1}, X可以是零或者有一个 |
X+ | 相当于X{1,} ,X至少有一个 |
X* | X至少有零个 |
上面Helloworld的例子改写
public static void main(String[] args) {
String rule = "[a-zA-z]*[0-9]?";
String a = "Helloworld1";
String b = "Noway2";
//用正则表达式检查是不是两个内容一样
boolean check1 = a.matches(rule);
boolean check2 = b.matches(rule);
System.out.println(check1);//true
System.out.println(check2);//true
}
这次两个都是true的原因,"[a-zA-z]*[0-9]?"代表着字母超过1个,数字0到1个,所以他们都符合了。
但是[a-zA-z]写的的太麻烦,是不是还有更简便的方法呢?答案是有的
规则 | 正则表达式 | 实际代码 |
---|---|---|
匹配一个数字 | \d | \\d |
匹配一个字母和0-9的数字 | \w | \\w |
匹配一个空白字符 | \s | \\s |
匹配一个非数字 | \D | \\D |
匹配一个非空白 | \S | \\S |
匹配一个非单词字符 | \W | \\W |
匹配任意一个字符 | . | . |
匹配一个汉字 | [\u4e00-\u9fa5] | [\u4e00-\u9fa5] |
注意:
写的代码里面记得需要多加一个“\”
那Helloworld可以改成
public static void main(String[] args) {
String rule = "\\w*\\d?";
String a = "Helloworld1";
String b = "Noway2";
//用正则表达式检查是不是两个内容一样
boolean check1 = a.matches(rule);
boolean check2 = b.matches(rule);
System.out.println(check1);//true
System.out.println(check2);//true
}
"\w*\d?"代表着是一个以上的字母(不论大小写)和一个或者零个数字,所以全部符合,输出位true
特殊字符转义
特殊字符需要转义,如下:
规则 | 正则表达式 | 实际代码 |
---|---|---|
. | \. | \\. |
[ | \[ | \\[ |
? | \? | \\? |
* | \* | \\* |
+ | \+ | \\+ |
\ | \\ | \\\ |
举个例子说明:
假如我们需要识别网址,例如www.baidu.com
public static void main(String[] args) {
//检查是不是www.baidu.com之类的网址
String rule = "\\w{3}\\.\\w{5}\\.\\w{3}";
String website = "www.baidu.com";
boolean judge = website.matches(rule);
System.out.println(judge);//true
}
"\w{3}\.\w{5}\.\w{3}"代表着3个字母,一个点,5个字母,1个点和三个字母
Split
split将一个字符串分开为几个子字符串,返回值是String类型数组。
例子
public static void main(String[] args) {
String website = "www.baidu.com";
String[] str = website.split("\\.");
for(int i=0;i< str.length;i++){
System.out.println(str[i]);//输出三个结果www baidu com
}
}
注意这里是由".“分开的,因为它是特殊字符需要转义,所以我写了”\."
replaceAll
从上述描述来看写法是replaceAll(需要替代的词,替换字符串) 返回值是String字符串类型
举个例子:
public static void main(String[] args) {
String str = "呵1呵2咯3咯4哈5哈777,替代语气词";
String s = str.replaceAll("[呵咯哈]", "A");
System.out.println(s);//输出是:A1A2A3A4A5A777,替代语气词
}
上面例子,只要遇见呵咯哈就替换成"A".