1. 正則表達式語法
使用工具 :RegexBuddy.exe 正則匹配
1. 標準字符集合
\n: 換行符
\t: 製表符
\\ \^ \$: 需要特殊轉義的
\d : 數字 0-9 任意一個數字
\D : 非數字
\w : 字母 A-Z,a-z,_ 任意一個字母或下劃線
\W: 非 字母 A-Z,a-z,0-9,_
\s: 空格、製表符、換行符等空白字符其中任意一個
.: 匹配任意字符(除了換行符)
包括任何字符:[\s\S]
2. 自定義字符集合
[] 方括號,匹配反括號中任意一個字符
[abc@]: 匹配a,b,c,@ 任意一個字符, 裏面是 a或b或c 的意思
[^abc]: 匹配 a,b,c 之外 任意一個字符
[a-z] : 匹配 a-z 之間 任意一個字母
[^a-f0-3]: a-f 0-3 之外的任意一個字符
注意: 正則表達式的特殊符號在[]內,除了^,-之外 都是去了 特殊含義,
比如[.] 就是表示.
3. 量詞:
{n}: 表示重複 n次
{m,n}: 至少重複m次,最多重複n 次
{m,}: 表達式至少重複m次
? : 表達式 0次或者 1次 ,相當於 {0,1}
匹配前面的子表達式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等價於 {0,1}
+ : 表達式出現1 次,或者任意次,相當於 {1,}
* : 表達式不出現或者出現任意次,相當於{0,}
例子:\d{3,6} 表示第一個數字開頭,可以出現3-6次 2333 5666 比如
貪婪模式:
\d{3,6} : 默認是 \d{3,6}! 匹配字符越多越好,比如 123456 從1開始匹配到6
非貪婪模式:
\d{3,6}: \d{3,6}? , 匹配字符越少越好, 123456從1開始只匹配 123重新開始匹配 456
4. 字符邊界,匹配的是符合某一個條件的位置
^ : 與字符開始地方匹配
$ : 與字符結束地方匹配
\b: 前面 或者後面 不是 \w
匹配一個單詞邊界 ,匹配單詞 左右 邊界
5.
A|B: 匹配A 或 B
6. 對正則表達式結果進行 過濾 正則表達式斷言
[a-z]+(?=\d+)
[a-z]+(?!\d+)
電話號碼匹配: 0\d[2,3]-\d[7,9]
手機號碼 : (1[35789]\d{9})|(0\d{2,3}-\d{7,9})
郵箱匹配:
[\w\-]+@[a-z0-9A-Z]+(\.[A-Za-z]{2,4}){1,2}
110 @ qq.com.cn
2. 正則表達式使用
2.1. 開發工具中使用:
基本開發工具支持正則表達式查找 notepad++
2.2 數據庫使用:
數據庫 也可以支持正則表達式 ,mysql5.5 oracle 10g
例子:select prod_name from products where prod_name REGEXP '.000'
2.3. java 程序中 正則表達式
2.3.1. 使用 正則表達式 對象 全局匹配 ,逐個匹配
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class TestPartern {
@Test
public void test1(){
// 使用 正則表達式 對象 全局匹配 ,逐個匹配
Pattern p= Pattern.compile("\\d{2}");
// 創建 Matcher 對象
Matcher m= p.matcher("2998775451");
// 嘗試將 整個 字符串序列 與該模式匹配
boolean yesorno= m.matches();
System.out.println(yesorno); // 匹配成功 true
while(m.find()){
/*** 逐個匹配
* 98
77
54
51
*/
System.out.println(m.group());
}
}
}
2.3.2. 分組匹配
public void test2(){
// 正則表達式 對象
Pattern p= Pattern.compile("(\\d+)(\\w+)");
// 創建 Matcher 對象
Matcher m= p.matcher("2998775451xiaoming");
// 嘗試將 整個 字符串序列 與該模式匹配
// boolean yesorno= m.matches();
while(m.find()){
// 獲取匹配 結果
System.out.println(m.group()); //2998775451xiaoming
System.out.println(m.group(1)); //第一組匹配內容:(\\d+) 2998775451
System.out.println(m.group(2)); //第二組匹配內容:(\\w+) xiaoming
};
// System.out.println(yesorno);
}
2.3.3 字符串 替換、 分割
// 字符串 替換、 分割
@Test
public void test3(){
// 正則表達式 對象
Pattern p= Pattern.compile("[0-9]");
// 創建 Matcher 對象
Matcher m= p.matcher("[email protected]");
// 替換 ##########@qq.com
String newStr= m.replaceAll("#");
System.out.println(newStr);
// 分割
String str="a12b12x445d";
String[] arrs= str.split("\\d+");
System.out.println(Arrays.toString(arrs));
}
2.3.4 模仿爬蟲,取出圖片
/**
* 模仿爬蟲,取出圖片
* @throws IOException
*/
@Test
public void test4() throws IOException{
URL url=new URL("https://www.baidu.com/");
StringBuilder sb=new StringBuilder();
BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream()));
String temp="";
while( (temp = reader.readLine())!=null){
// System.out.println(temp);
sb.append(temp);
}
// System.out.println(sb.toString());
// Pattern p= Pattern.compile("<a.+</a>");
// Pattern p= Pattern.compile("<a[\\s\\S]+?</a>");
// <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a>
// <a[\\s\\S]+ 匹配出現 0 次或者 1 次 ,避免 超鏈接 連續
Pattern p= Pattern.compile("<a[\\s\\S]+?</a>");
Matcher m= p.matcher(sb.toString());
while(m.find()){
System.out.println("超鏈接是:"+m.group());
}
}
// 從 a 標籤中取出 鏈接地址 正則表達式
@Test
public void test5(){
Pattern p2= Pattern.compile("href=[\\s\\S]+(.com)");
Matcher m2= p2.matcher("<a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a>");
if(m2.find()){
System.out.println(m2.group());
}
}