在线测试工具 http://tool.oschina.net/regex/
正则,用于检测字符串是不是符合规则的方法。
字符串,一共两种情况:字符本身、字符位置
检查符合规则,提供两种操作:检查是否符合、提取符合结果
Java写法:
Patternp = Pattern.compile("\\d+");
Matcherm = p.matcher("heo123");
if(m.find())
System.out.println(m.group());
else
System.out.println("无");
打印结果:123
API说明:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/util/regex/Pattern.html
正文开始:
匹配,当然有两种:精准匹配、模糊匹配。
1. 精准匹配:
"hello",
匹配"hello"为true;
匹配"hello1"为false,但matcher.find()返回为true,matcher.group()返回为“hello”
匹配"h1ello"为false, false, 报异常:java.lang.IllegalStateException:No match found
2. 模糊匹配:
精准匹配没什么意思,助你理解。重点是这个模糊匹配:
模糊为两种:一个字符可以有几个、一个位置可以是什么字符。
2.1 一个字符可以有几个?
"hel{1,3}o",
可以匹配的字符串有:helo、hello、helllo
2.2 一个位置可以是什么字符?
这里先说“一个”位置,再说“连续多个”位置
2.2.1 一个位置:
“hell[a,o]”,
可以匹配的字符串有:hella、hello
1) 我想匹配更多,比如:a-z都要可以匹配,即希望hella\hellb\hellc\...\hellz
hell[a-z],
但区分大小写,hellA是不行的
2) 我大小写都想要:
hell[a-zA-Z]
3) 数字也想要
hell[a-zA-Z0-9]
4) 只想要数字了
hell[0-9]
5) 除此之外我还想要"-"
hell[0-9\\-] 或 hell[0-9-] 反正注意-有特殊使命,别像上面那样写在范围的中间
6) 我想要空白字符,什么\b\t\n\f\r的尽管来:
额,那就hell[\b\t\n\f\r]得了呗
\t | 制表符 ('\u0009') |
\n | 新行(换行)符 ('\u000A') |
\r | 回车符 ('\u000D') |
\f | 换页符 ('\u000C') |
7) 我不要[a,b,c]其它的什么字符都行
hell[^a-c]
8) 那我只要[a-z]但不要bc
hell[a-z&&[^bc]]
预定义字符类 | |
. | 任何字符(与行结束符可能匹配也可能不匹配) |
\d | 数字:[0-9] |
\D | 非数字: [^0-9] |
\s | 空白字符:[ \t\n\x0B\f\r] |
\S | 非空白字符:[^\s] |
\w | 单词字符:[a-zA-Z_0-9] |
\W | 非单词字符:[^\w] |
注意,以上的模式字串,只能匹配一个字符。
2.2.2 多个位置:
1) 我让hell字符串后面可以随便有3个英文或数字字符:
hell[a-zA-Z0-9]{3}
2)这三个字符写不写都不打紧,但不能超过3个:
hell[a-zA-Z0-9]{0,3} //注意,多于3个字符时,find()能找到,group()结果是只有3个的,但Pattern.matches("hell[a-zA-Z0-9]{0,3}","hellb1b3");的结果是false
3)随便几个字符都行了,不必强求3个,但至少得有一个:
hell[a-zA-Z0-9]+
4)那一个也没有也行:
hell[a-zA-Z0-9]?
5)重点来了:
Greedy 数量词 | |
X? | X,一次或一次也没有 |
X* | X,零次或多次 |
X+ | X,一次或多次 |
X{n} | X,恰好 n 次 |
X{n,} | X,至少 n 次 |
X{n,m} | X,至少 n 次,但是不超过 m 次 |
看看这里,注意分析:
regix="\d{2,5}"||string="123"||result=123
regix="\d{2,5}"||string="1234"||result=1234
regix="\d{2,5}"||string="12345"||result=12345
regix="\d{2,5}"||string="123456"||result=12345 //不加问号,则贪婪匹配,能多匹配点就多匹配点
regix="\d{2,5}?"||string="123"||result=12 //在数量词后面加问号,则惰性匹配
regix="\d{2,5}?"||string="1234"||result=12
regix="\d{2,5}?"||string="12345"||result=12
regix="\d{2,5}?"||string="123456"||result=12
regix="\d{2,}?"||string="123"||result=12
regix="\d{2,}?"||string="1234"||result=12
regix="\d{2,}?"||string="12345"||result=12
regix="\d{2,}?"||string="123456"||result=12
regix="\d+?"||string="123"||result=1
regix="\d+?"||string="1234"||result=1
regix="\d+?"||string="12345"||result=1
regix="\d+?"||string="123456"||result=1
3.边界匹配
// String regix = "^"; //#hello
// String regix = "$"; //hello#
// String regix = "^|$"; //#hello# #hello world#
// String string = "hello";
// String string = "hello world";
// String regix = "\\b"; //[#JS#] #Lesson_01#.#mp4#
// String regix = "\\B"; //#[J#S]# L#e#s#s#o#n#_#0#1.m#p#4
// String string = "[JS] Lesson_01.mp4";
//正向先行断言和负向先行断言 positive lookahead和negative lookahead
//白话解释:找出指定字符 前面 的位置,加!就是相反的位置
// String regix = "(?=l)"; //he#l#lo
// String regix = "(?=e)"; //h#ello
// String regix = "(?!l)"; //#h#ell#o#
// String string = "hello";
//环视,即看看右边或看看左边
//白话解释:找出指定字符 后面 的位置,加!就是相反的位置
// String regix = "(?<=l)"; //hel#l#o
// String regix = "(?<=e)"; //he#llo
// String regix = "(?<!l)"; //#h#e#llo#
// String regix = "(?<!e)"; //#h#el#l#o#
//没有以下这种写法
// String regix = "(?>=l)"; //hello
// String regix = "(?>=e)"; //hello
// String regix = "(?>!l)"; //hello
// String regix = "(?>!e)"; //hello
// String string = "hello";
// String regix = "(?=\\d{3})+"; //,1,2,3,4,5,678 ,1,2,3,4,5,6,789
// String regix = "(?=(\\d{3})+)"; //,1,2,3,4,5,678 ,1,2,3,4,5,6,789
// String regix = "(?=\\d{3}\\b)+"; //12345,678 123456,789
// String regix = "(?=(\\d{3})+\\b)"; //12,345,678 ,123,456,789
// String regix = "(\\B)(?=(\\d{3})+\\b)";
// String string = "12345678 123456789";
// Pattern p = Pattern.compile(regix);
// Matcher m = p.matcher(string);
// String a = m.replaceAll(",");
// //System.out.println(m.group());
// System.out.println(a);