一、有关的正则表达式符号
预定义字符类
. | 任何字符(与行结束符可能匹配也可能不匹配) | |
\d | 数字:[0-9] | |
\D | 非数字: [^0-9] | |
\s | 空白字符:[ \t\n\x0B\f\r] | |
\S | 非空白字符:[^\s] | |
\w | 单词字符:[a-zA-Z_0-9] | |
\W | 非单词字符:[^\w] | |
System.out.println("a".matches(".")); System.out.println("1".matches("\\d")); System.out.println("%".matches("\\D")); System.out.println("\r".matches("\\s")); System.out.println("^".matches("\\S")); System.out.println("a".matches("\\w")); |
Greedy 数量词
X? | X,一次或一次也没有 |
X* | X,零次或多次 |
X+ | X,一次或多次 |
X{n} | X,恰好n次 |
X{n,} | X,至少n次 |
X{n,m} | X,至少n次,但是不超过m次 |
System.out.println("a".matches(".") ); System.out.println("a".matches("a") ); System.out.println("a".matches("a?") ); System.out.println("aaa".matches("a*") ); System.out.println("".matches("a+") ); System.out.println("aaaaa".matches("a{5}") ); System.out.println("aaaaaaaaa".matches("a{5,8}") ); System.out.println("aaa".matches("a{5,}") ); System.out.println("aaaaab".matches("a{5,}") ); |
范围表示
[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](减去) |
|
|
System.out.println( "a".matches("[a]") ); System.out.println( "aa".matches("[a]+") ); System.out.println( "abc".matches("[abc]{3,}") ); System.out.println( "abc".matches("[abc]+") ); System.out.println( "dshfshfu1".matches("[^abc]+") ); System.out.println( "abcdsaA".matches("[a-z]{5,}") ); System.out.println( "abcdsaA12".matches("[a-zA-Z]{5,}") ); System.out.println( "abcdsaA12".matches("[a-zA-Z0-9]{5,}") ); System.out.println( "abdxyz".matches("[a-c[x-z]]+")); System.out.println( "bcbcbc".matches("[a-z&&[b-c]]{5,}")); System.out.println( "tretrt".matches("[a-z&&[^b-c]]{5,}")); |
1、需求:获取由3个字母组成的单词。
public static void getDemo()
{
String str = "da jia zhu yi le,ming tian bu fang jia,xie xie!";
//想要获取由3个字母组成的单词。
//刚才的功能返回的都是一个结果,只有split返回的是数组,但是它是把规则作为分隔符,不会获取符合规则的内容。
//这时我们要用到一些正则对象。
String reg = "\\b[a-z]{3}\\b";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find())
{
System.out.println(m.start()+"...."+m.end());
System.out.println("sub:"+str.substring(m.start(),m.end()));
System.out.println(m.group());
}
// System.out.println(m.find());//将规则对字符串进行匹配查找。
// System.out.println(m.find());//将规则对字符串进行匹配查找。
// System.out.println(m.group());//在使用group方法之前,必须要先找,找到了才可以取。
}
2、校验邮件
public static void checkMail()
{
String mail = "abc123@sina.com.cn";
mail = "1@1.1";
String reg = "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
reg = "\\w+@\\w+(\\.\\w+)+";//简化的规则。笼统的匹配。
boolean b = mail.matches(reg);
System.out.println(mail+":"+b);
}
3、网络爬虫
class GetMailList
{
public static void main(String[] args) throws Exception
{
String reg = "\\w+@[a-zA-Z]+(\\.[a-zA-Z]+)+";
getMailsByWeb(reg);
}
public static void getMailsByWeb(String regex)throws Exception
{
URL url = new URL("http://localhost:8080/myweb/mail.html");
URLConnection conn = url.openConnection();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
Pattern p = Pattern.compile(regex);
while((line=bufIn.readLine())!=null)
{
//System.out.println(line);
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
bufIn.close();
}
public static void getMails(String regex)throws Exception
{
BufferedReader bufr =
new BufferedReader(new FileReader("mail.txt"));
String line = null;
Pattern p = Pattern.compile(regex);
while((line=bufr.readLine())!=null)
{
//System.out.println(line);
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
bufr.close();
}
}