代码总结:
package com.atguigu.chapter13;
//超哥nb
import java.util.Arrays;
public class RegExpDemo {
/*
正则表达式: Regular expression
贪官 字符串 "123456"
法律 正则表达式 \\d+
Pattern 表示正则表达式, 经过编译得到正则表达式
Matcher 匹配器
-----
java在字符串提供了四个方法, 直接支持正则表达式
matches
replaceAll
replaceFirst
split
正则语法:
[ab] a或者b
[a-z] 所有小写字母
[a-zA-Z] 所有字母
[a-zA-Z0-9_] 数字字母下户线 单词字符串
[^ab] 不是a也不是b ^放在方括号中, 表示非
\w word: 单词字符串 [a-zA-z0-9_]
\W 非单词字符串 [^\w]
\d d:digital [0-9]
\D 非数字
\s s:space 空白字符 : 空格 \r \n \t
. 表示任意字符串 除了: \r \n
\. 表示真正的点
^ 开头
$ 结尾
---
数量词:
a{2} 正好2个a
a{2,} 至少2个
a{2,5} 至少2个 最多5个
a+ 至少一个 a{1,}
a* 至少一个 a{0,}
a? 0个或1个
abc|d abc或d
ab(c|d) c或者d
\1 取第一组
$1 取第一组的字符
*/
public static void main(String[] args) {
/*String s = "1860307163";
boolean b = s.matches("1[3-9]\\d{9}");
System.out.println(b);*/
/*String s = "11a@qq.com.cn";
boolean b = s.matches("\\w{3,15}@\\w+\\.(com|cn|org|edu|com\\.cn)");
System.out.println(b);*/
/*String s = "aslfjal3209475falfj32o4u";
System.out.println(s.replaceAll("\\d+", ""));
System.out.println(s.replaceFirst("\\d+", ""));*/
/*String s = "我我我今今今天要要要去去洗脚";
System.out.println(s.replaceAll("(.)\\1+", "$1"));*/
// 切割的的时候, 中间和前面的空字符串保留, 末尾的忽略
/*String s = "jfadslfjasldaa123bb22cc3abcfjdslfjlas33";
String[] data = s.split("\\D");
System.out.println(Arrays.toString(data));*/
String ip = "192.168.1.2";
String[] data = ip.split("\\.");
System.out.println(Arrays.toString(data));
}
}
java特殊字符转义问题(在其他语言中不用考虑该影响!!!)

故:从字符串到正则匹配特殊字符经历了两次转义
1.java转义:pattern.compile-->将字符编译成pattern对象
2.正则转义;p.matcher(input)-->转义成正则的字符,再去匹配