来源: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;
}
}