來源:http://www.bjsxt.com/
一、S03E254_01正規表達式01_介紹、标準字元集合、自定義字元集合
Regular Expression或Regex
RegexBuddy
文法(1)
文法(2)
文法(3)
二、S03E255_01正規表達式02_量詞、貪婪和非貪婪模式
文法(4)
幾個常用的非貪婪比對Pattern
*? 重複任意次,但盡可能少重複
+? 重複次或更多次,但盡可能少重複
?? 重複次或次,但盡可能少重複
{n,m}? 重複n到m次,但盡可能少重複
{n,}? 重複n次以上,但盡可能少重複
三、S03E256_01正規表達式03_字元邊界、比對模式(單行和多行模式)
文法(5)
正規表達式的比對模式
四、S03E257_01正規表達式04_分支結構、捕獲組、非捕獲組、反向引用
文法(6)
五、S03E258_01正規表達式05_預搜尋、零寬斷言(4個文法結構)
文法(7)
六、S03E259_01正規表達式06_電話号碼、手機号碼、郵箱、常用表達式
練習1
練習2
網絡上的
七、S03E260_01正規表達式07_正規表達式、開發環境、文本編輯器中使用
八、S03E261_01正規表達式08_正規表達式、JAVA程式設計中使用、查找、替換、分割
package com.test.regularExpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 測試正規表達式對象的基本用法
*/
public class Demo01 {
public static void main(String[] args) {
//在這個字元串:ssf9798,是否符合指定的正規表達式:\w+
//表達式對象
Pattern p = Pattern.compile("\\w+");
//建立Matcher對象
// Matcher m = p.matcher("ssf99798"); //嘗試将整個字元序列與該模式比對
Matcher m = p.matcher("ssss9&&oou433");
boolean yesorno = m.matches();
System.out.println(yesorno); //ssf99798為true,ssss9&&oou433為false
boolean yesorno2 = m.find(); //掃描輸入的序列,查找與該模式比對的下一個子序列
//m.matches()為true,比對完了,則m.find()從字元序列最後位置比對,為false
//m.matches()為false,則m.find()從字元序列開始位置比對,為true
System.out.println(yesorno2); //目前為true
System.out.println("=====單單測試find()======================================");
Pattern p2 = Pattern.compile("\\w+");
Matcher m2 = p2.matcher("ssss9&&oou433");
// System.out.println(m2.find()); //true
// System.out.println(m2.find()); //true
// System.out.println(m2.find()); //false
while(m2.find()){
//group()和group()都是比對整個表達式的子字元串,是以列印内容一樣
//第一次列印ssss9,第二次列印oou433
System.out.println(m2.group());
System.out.println(m2.group());
}
}
}
package com.test.regularExpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 測試正規表達式對象中分組的處理
*/
public class Demo02 {
public static void main(String[] args) {
Pattern p = Pattern.compile("([a-z]+)([0-9]+)"); //有兩個分組:([a-z]+) 與 ([0-9]+)
Matcher m = p.matcher("df43**dfdf545**fdg99");
while(m.find()){
System.out.println("====整個子序列=============");
System.out.println(m.group());
System.out.println("========子序列的分組===================");
System.out.println(m.group()); //([a-z]+)
System.out.println(m.group()); //([0-9]+)
}
}
}
package com.test.regularExpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 測試正規表達式對象的替換字元串操作
*/
public class Demo03 {
public static void main(String[] args) {
Pattern p = Pattern.compile("[0-9]");
Matcher m = p.matcher("aa33**dfd89*dfd87");
//替換
String newStr = m.replaceAll("#");
System.out.println(newStr); //aa##**dfd##*dfd##
}
}
package com.test.regularExpression;
import java.util.Arrays;
/**
* 測試正規表達式對象的分割字元串操作
*/
public class Demo04 {
public static void main(String[] args) {
String str = "a,b,c";
String[] arrs = str.split(",");
System.out.println(Arrays.toString(arrs)); //[a, b, c]
String str2 = "a9879b8978c9768";
String[] arrs2 = str2.split("\\d+");
System.out.println(Arrays.toString(arrs2)); //[a, b, c]
}
}
九、S03E262_01正規表達式09_正規表達式、手寫網絡爬蟲、基本原理、亂碼處理
package com.test.regularExpression;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 網絡爬蟲取連結(可以不用自己寫,已有相關産品,如wget)
*/
public class WebSpider {
public static void main(String[] args) {
String destResult = getURLContent("http://www.163.com","gbk");
List<String> result = getMatcherSubStr(destResult, "href=\"([\\w\\s./:]+?)\"");
for (String temp : result) {
System.out.println(temp);
}
}
/**
* 獲得urlStr對應網頁的源碼内容
* @param urlStr
* @return
*/
public static String getURLContent(String urlStr,String charset){
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(urlStr);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(),Charset.forName(charset)));
String temp = "";
while((temp=reader.readLine())!=null){
// System.out.println(temp);
sb.append(temp);
sb.append("\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static List<String> getMatcherSubStr(String destStr,String regexStr){
Pattern p = Pattern.compile(regexStr); //取到的超連結的位址
Matcher m = p.matcher(destStr);
List<String> result = new ArrayList<String>();
while(m.find()){
result.add(m.group());
}
return result;
}
}