一:正则表达式用途:
1)字符串匹配;2)字符串查找、提取;3)字符串替换、分割
例如:验证IP、电话号码、邮箱、用户名、密码;从网页中查找email地址、链接等
二:所涉及的类:
1) java中的正则位于java.util.regex中 2)java.lang.String;3)java.util.regex.Pattern;4)java.util.regex.Matcher
三:常用字符含义:
字符 | 含义 |
$ | 结束标志 |
^ | 开始标志 |
() | 标记子表达式的开始结束 |
[] | 中括号表达式的开始结束 |
{} | 前面表达式出现的频度 |
* | 前面表达式出现零次或多次 |
+ | 前面表达式出现一次或多次 |
? | 前面表达式出现零次或一次 |
| | 任选 |
. | 匹配任何字符 |
\d | 0-9的所有数字 |
\D | 非数字 |
\s | 所有空白字符(空格、制表符、回车符、换页符、换行符等) |
\S | 所有非空白字符 |
\w | 所有字符单词(0-9数字、26个字母、下划线_) |
\W | 所有非字符单词 |
{n} | 前面表达式出现n次 |
{n,} | 前面表达式出现至少n次 |
{n,m} | 前面表达式至少出现n次不超过m次 |
四:
1)枚举(例如:[abc]:a、b、c任意字符)
2) 范围(例如:[a-z]:a到z间任意字符或[a-fh-z]:a到f和h到z间任意字符)
3)常用正则表达式如:
@匹配电话号码的正则表达式:final String USERPHONE="^((13[0-9])|(15[^4])|(18[0,2,5-9]))\\d{8}$";
@匹配密码的正则表达式:final String USERPASSWARD="^[a-zA-Z]\\w{5,17}$";(以字母开头,长度在6~18之间,包含字符/数字/下划线)
@匹配邮箱的正则表达式:final String USERMAIL="^[A-Za-z0-9][\\w\\-\\.]{3,12}@([\\w\\-]+\\.)+[\\w]{2,3}$";(以英文字母、数字开头,账号4-13个字符,字母/数字/短横线,以2到3个字母/数字结尾)
4)数量标识符模式:
@贪婪模式(Greedy):一直匹配,直到不能匹配,默认都是
@勉强模式(Reluctant):匹配最少的字符,用?表示
@占有模式(Possessive):不保存在匹配过程中产生的中间状态,匹配失败便不会原路返回,可以防止正则表达式的失控,提高运行效率。
各模式下字符的含义如下:
Greedy | Reluctant | Possessive | 含义 |
? | ?? | ?+ | 前面表达式出现零次或一次 |
* | *? | *+ | 零次或多次 |
+ | +? | ++ | 一次或多次 |
{n} | {n}? | {n}+ | n次 |
{n,} | {n,}? | {n,}+ | 至少n次 |
{n,m} | {n,m}? | {n,m}+ | 至少n次不超过m次 |
五:常用方法的使用
1)字符串匹配:
Pattern p=Pattern.compile("[a-z]{3}");//模式,将正则表达式编译成Pattern对象
Matcher m=p.matcher("cyj");//匹配结果存放在Matcher类的对象中
boolean b=m.matches();//Matcher中的一个方法,返回目标表达式与正则表达式是否匹配
System.out.println(b);
2)Matcher中几个常用的方法:
boolean find():返回是否存在包含于与Pattern匹配的子串
Pattern p=Pattern.compile("[a-z]{3}");
Matcher m1=p.matcher("cyjddd");
Matcher m2=p.matcher("c");
boolean b1=m1.find();
boolean b2=m2.find();
System.out.println(b1);//true
System.out.println(b2);//false
String group():返回与Pattern匹配的子串
int start():返回与Pattern匹配的子串在字符串中开始的位置
int end():int start():返回与Pattern匹配的子串在字符串中结束的位置+1
Pattern p=Pattern.compile("[a-z]{3}");
Matcher m=p.matcher("cfff,ssfssdd-ds");
while(m.find()){
System.out.println(m.group());//cff ssf ssd
System.out.println(m.start());//0 5 8
System.out.println(m.end());//3 8 11
}
boolean lookingAt();返回字符串前面部分与Pattern是否匹配
Pattern p=Pattern.compile("[a-z]{3}");
Matcher m1=p.matcher("cfff,ssfssdd-ds");
Matcher m2=p.matcher("cf,ssfssdd-ds");
System.out.println(m1.lookingAt());//true
System.out.println(m2.lookingAt());//false
boolean matches();返回整个字符串与Pattern是否匹配
Pattern p=Pattern.compile("[a-z]{3}");
Matcher m=p.matcher("cff,ssfssdd-ds");
System.out.println(m.matches);//false
reset():重置匹配位置
Pattern p=Pattern.compile("[a-z]{3}");
Matcher m=p.matcher("fff,ssfssdd-ds");
if(m.find()){
System.out.println(m.start());//0
System.out.println(m.group());//fff
System.out.println(m.reset());//java.util.regex.Matcher[pattern=[a-z]{3} region=0,14 lastmatch=]
}
if(m.find()){
System.out.println(m.start());//0
System.out.println(m.group());//fff
}
String[] split(String regex):字符串分割
String[] strs="sd/dd-sss,hh,jff,jj".split("[a-z]{3}");
for(String s:strs){
System.out.println(s);
}// sd/dd- ,dd, ,jj
六:正则表达式是字符处理很好的工具,缩写为regex,单数有regex、regexp,复数为regexps、regexes、regexen.