一、什么是正则表达式
正则表达式是一种字符串的匹配
用来解决:
字符串匹配(字符匹配)
字符串查找
字符串替换
例如
检验IP地址是否正确
从网页中抓取email地址
从网页中抓取连接
需要用到的类
java.lang.String
java.util.regex.Matcher
java.util.regex.Patter
使用方法
指定为字符串的正则表达式必须首先被编译为Pattern类的实例,这样便创建一个Pattern对象(一个标准或者说模式),可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
因此,典型的调用顺序是
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaaab”);
boolean b = m.matches();
二、常见表达式
1、初识.*+?
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
看一些打印结果:
print("a".matches(".")); TRUE
print("aa".matches("aa")); TRUE
print("Aa".matches("aa")); FALSE
print("aaaa".matches("a*")); TRUE
print("".matches("a*")); TRUE
print("aaaa".matches("a+")); TRUE
print("".matches("a+")); FALSE
print("aaaa".matches("a?")); FALSE
print("a".matches("a?")); TRUE
print("".matches("a?")); TRUE
print("345".matches("\\d{3,100}")); true
print("192.168.1.103".matches("\\d{1,3}\\.\\d{1,3}.\\d{1,3}\\.\\d{1,3}")); true
print("192".matches("[0-2][1-9][1-9]")); true
2、 范围(字符类)
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
print("a".matches("[abc]")); //简单类true
print("a".matches("[^abc]")); //除字符abc所组成的类false
print("a".matches("[a-zA-Z]")); //求并集true
print("A".matches("[a-z]|[A-Z]"));//求并集true
print("A".matches("[a-z[A-Z]]")); //求并集true
print("a".matches("[a-z]&&[A-Z]")); //求交集 false
print("&^".matches("[&^%]+"));//true
print("&^".matches("[&^%]"));//false 该方法是判断一个字符是否存在与模式中。多个字符判断,后面加上“+”。
3’\s\w\d和\S\W\D
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
print("&^".matches("[&%^]+"));//true
print(" \n\r\t".matches("\\s{4}"));//true
print(" ".matches("\\S"));//false
print("a_8".matches("\\w{3}"));//true
print("abc81&^%".matches("[a-z]{1,3}\\d+[&^%]+"));\\true
print("\\".matches("\\\\"));\\true
4、posic
5、边界
^ 行的开头
$ 行的结尾
\b 单词边界
\B 非单词边界
\A 输入的开头
\G 上一个匹配的结尾
\Z 输入的结尾,仅用于最后的结束符(如果有的话)
\z 输入的结尾
print("hello sir".matches("^h.*"));//以h开头,以字符节true
print("hello sir".matches(".*ir$"));//字母开头,ir结尾true
print("hello sir".matches("^[a-z]{1,4}o\\b.*"));//b单词边界true
print("hellodsir".matches("^[a-z]{1,4}o\\B.*")); //B非单词边界 true
6、统计有多少个空白行
print(" \n".matches("^[\\s&&[^\\n]]*\\n$"));//以除了开头,并且以"\n"结尾的行
7、find()、lookingAt()
matcher():是比较整个字符串
find():比较子串,自要子串满足,匹配为true.子串满足后,需调用reset方法,继续比较。
lookingAt():从字符串开头与该模式匹配,也是比较的子串。
Pattern p = Pattern.compile("\\d{3,5}");
String s = "123-456-2365-00";
Matcher m = p.matcher(s);
print(m.matches());//false:比较的是整个字符串
m.reset();//重置
print(m.find());//比较子串true
print(m.start()+"-"+m.end());//0-3包含“-”
print(m.find());//比较子串true
print(m.start()+"-"+m.end());//4-7
print(m.find());//比较子串true
print(m.start()+"-"+m.end());//8-12
print(m.find());//比较子串 false
print(m.lookingAt());//字符串开头与该模式匹配比较子串:true
print(m.find());//true
print(m.find());//true
print(m.find());//false
9、replacement字符替换
10、group
Pattern p = Pattern.compile("\\d{3,5}[a-z]{2}");
String s = "123aa-234bb-345yy-34dffg-fgfg";
Matcher m = p.matcher(s);
print(m.matches());
m.reset();
while(m.find()){
print(m.group());
}
以上能够输出和P匹配的子串,123aa 234bb 345yyn但如果只想输出123 234 345 。可以利用分组来解决。这里将数字分为一组,字母分为一组,将数字输出。
Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
String s = "123aa-234bb-345yy-34dffg-fgfg";
Matcher m = p.matcher(s);
print(m.matches());
m.reset();
while(m.find()){
print(m.group(1));
}
需求:抓取网页中的mail信息
import java.io.*;
import java.util.regex.*;
public class GetMailaddress {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
File file = new File("E:/test/test.html");
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = "";
final String regex = "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+";
Pattern p = Pattern.compile(regex);
while((line = bufr.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}
}
}