代码改变世界

Java正则表达式学习

2013-01-21 17:29  littlelion  阅读(414)  评论(0编辑  收藏  举报

---恢复内容开始---

在学习Junit时,发现需要再补充下正则表达式的东西,以前略知一二(真的只是一二而已,例如.*   ),但没有系统地学习过,现在要从头学习下。

关于Java正则表达式的各种符号列表就略过了,网上一坨坨的。

Java自带了regex包,包含了两个类:Pattern 和 Matcher

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

用法:

  • Pattern p = Pattern.complie(regex);  //构造一个regex模式
  • Matcher m = p.matcher(str);            //构造一个匹配器
  • boolean b = m.matches();                //判断是否匹配
  • 1  判断一个字符串是否含有数字

String str1 = "aas0";
String regex1 = ".*\\d.*"; //.* 匹配任意数量个字符
Pattern p1 = Pattern.compile(regex1);
Matcher m1 = p1.matcher(str1);
System.out.print(m1.matches());

注: .* 匹配任意数量任意字符  \d 数字,相当于[0-9]

  • 2  判断是否包含空格字符

String str2 = " nam e";
String regex2 = ".*\\s.*";
Pattern p2 = Pattern.compile(regex2);
Matcher m2 = p2.matcher(str2);
System.out.print(m2.matches());

注:  \s 是空格字符,表示space、tab、换行、换页、回车

  • 3  匹配字符串的第一个字符

String str3 = "adkashdasf";
String regex3 = "^a.*";
Pattern p3 = Pattern.compile(regex3);
Matcher m3 = p3.matcher(str3);
System.out.print(m3.matches());

注:  ^ 要匹配第一个字符

  • 4  匹配第一个字符和最后一个字符

String str4 = "fesidk";
String regex4 = "^f.*k$";
Pattern p4 = Pattern.compile(regex4);
Matcher m4 = p4.matcher(str4);
System.out.print(m4.matches());

注:  $ 匹配最后一个字符

  • 5  匹配方括号内的字符

String str5 = "ertyabf";
Pattern p5 = Pattern.compile("..[wrt].*");
Matcher m5 = p5.matcher(str5);
System.out.print(m5.matches());

注: [wrt] 表示该处字符为方括号内的任意一个

  • 6  判断手机号以188开头
Pattern p6 = Pattern.compile("^[1][8][8]\\d{8}");
Matcher m6 = p6.matcher("18812312333");
Matcher m6_0 = p6.matcher("13811111111");
System.out.print(m6.matches()+" "+m6_0.matches());

注: {8} 表示重复8次,  \\d{8} 表示数字出现8次

  • 7  匹配中包含或者关系(匹配10个或15个数字的字符串)

Pattern p7 = Pattern.compile("\\d{10}|\\d{15}");
Matcher m7 = p7.matcher("1111111111");
Matcher m7_0 = p7.matcher("000000000");
Matcher m7_1= p7.matcher("222222222222222");
System.out.print("\n\rm7 = "+m7.matches()+"\n\rm7_0 = "+m7_0.matches()+"\n\rm7_1 = "+m7_1.matches());

 

  • 8  判断是否是邮箱
Pattern p8 = Pattern.compile("^[a-zA-Z]+([_.]?([a-zA-Z]|\\d))*@\\w+[.][c][o][m]|[c][n]$");
Matcher m8 = p8.matcher("dsd.s23_sd.33@aa5.com");
System.out.print(m8.matches()); //true

注:邮箱以字母开头,在字符之间的 下划线_  或点点.  只能出现一次,@之前必须是字母或数字,以.com或.cn结尾,邮箱名任意。

^[a-zA-Z]+  匹配一个或多个字母
[_.]? _或. 匹配一个或零个
\\w+ 匹配一个单词字符 [a-ZA-Z_0-9]
  • 9  匹配中文字符
Pattern p13 = Pattern.compile("^[\u4e00-\u9fa5]+$");
Matcher m13 = p13.matcher("大本钟");
System.out.print("\n"+m13.matches());
  •  10  find() start() end() group()方法

/*
 * find() Attempts to find the next subsequence of the input sequence that matches the pattern. 
 * start() Returns the start index of the previous match
 * end() Returns the offset after the last character matched
 * group() Returns the input subsequence matched by the previous match. 
 */
Pattern p14 = Pattern.compile("\\d+");
Matcher m14 = p14.matcher("aad333asd1234dsda");
while(m14.find()){
    System.out.println(m14.start());
    System.out.println(m14.end());
    System.out.println(m14.group();
}

运行结果:

3
6
333
9
13
1234
  •  11  split()用法

Pattern p15 = Pattern.compile("\\d+");
String str[] = p15.split("aa11bb234df31dwas09");
for(int i=0;i<str.length;i++)
    System.out.print(str[i]+" ");    //return : aa bb df dwas

 

未完待续...

 

---恢复内容结束---