);
Matcher matcher = pattern.matcher(content);
int i = 0;
while (matcher.find()) {
System.out.println("热搜"+(++i)+". "+matcher.group(1));
}
}
@Test
public void getRegexpExperienceIPAddress(){
content = "私有地址(Private address)属于非注册地址,专门为组织机构内部使用。\n" +
"以下列出留用的内部私有地址\n" +
"A类 10.0.0.0--10.255.255.255\n" +
"B类 172.16.0.0--172.31.255.255\n" +
"C类 192.168.0.0--192.168.255.255";
Pattern pattern = Pattern.compile("\\d+\\.\\d+\\.\\d+\\.\\d");
Matcher matcher = pattern.matcher(content);
int i = 0;
while (matcher.find()) {
System.out.println("IP地址"+(++i)+" "+matcher.group(0));
}
}
}
提出几个问题


正则表达式
正则表达式的引出

基本介绍
正则表达式的底层实现

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegTheory {
private String content ="1998年12月8日,第二代Java平台的企业版J2EE发布。1999年6月,Sun公司发布了"+
"第二代Java平台(简称为Java2)的3个版本:J2ME(Java2MicroEdition,Java2平台的微型"+
"版),应用于移动、无线及有限资源的环境;J2SE(Java2StandardEdition,Java2平台的"+
"标准版),应用于桌面环境;J2EE(Java2EnterpriseEdition,Java2平台的企业版),应"+
"用3443于基于Java的应用服务器。Java2平台的发布,是Java发展过程中最重要的一个"+
"里程碑,标志着Java的应用开始普及9889";
@Test
@SuppressWarnings({"all"})
public void regexpExperience02(){
String regStr = "\\d\\d\\d\\d";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到: " + matcher.group(0));
}
}
@Test
@SuppressWarnings({"all"})
public void regexpExperience03(){
String regStr = "(\\d\\d)(\\d\\d)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到: " + matcher.group(0));
System.out.println("第一组():" + matcher.group(1));
System.out.println("第二组():" + matcher.group(2));
System.out.println();
}
}
}
正则表达式语法
基本介绍

1.元字符转义号

代码示例:
package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience02 {
@Test
public void regexpExperience02(){
String content = "abc$()awg()123()";
String regStr = "\\(";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 "+matcher.group(0));
}
}
}
匹配.
@Test
public void regexpExperience03(){
String content = "abc$()awg.()123()";
String regStr = "\\.";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 "+matcher.group(0));
}
}
2.元字符-字符匹配符


\d代码示例
@Test
public void regexpExperience04(){
String content = "abc$()awg.()123()";
String regStr = "\\d{3}";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 "+matcher.group(0));
}
}
字符匹配符 示例
代码示例
package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience03 {
@Test
public void regexpExperience03(){
String content = "A22c8abcABCaBC";
String regStr = "a((?i)b)c";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("匹配到: " + matcher.group(0));
}
}
@Test
public void regexpExperience04(){
String content = "A22c8abcABCaBC";
String regStr = "bc";
Pattern pattern = Pattern.compile(regStr,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("匹配到: " + matcher.group(0));
}
}
@Test
public void regexpExperience05(){
String content = "A22c8abcABCaBC2";
String regStr = "[0-9]";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("匹配到: " + matcher.group(0));
}
}
}

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience04 {
@Test
public void RegexpExperience04(){
String content = "A22c8abcABCaBC2";
String regStr = "[^0-9]";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到\t"+matcher.group(0));
}
}
}

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience05 {
@Test
public void regexpExperience05(){
String content ="A 22ce.8,abc ABC_aBC 2";
String regStr = "\\.";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
}
2.元字符-选择匹配符

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience06 {
@Test
public void regexpExperience06(){
String content ="hanshunping韩顺平 寒";
String regStr = "han|韩|寒";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
}
3.元字符-限定符


举例说明


package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience07 {
@Test
public void regexpExperience07(){
String content = "1111111234aaahelloaaaa24";
String regStr = "\\d{2,5}";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到: "+matcher.group(0) );
}
}
}

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience08 {
@Test
public void regexpExperience07(){
String content = "a1111111234aaahelloaaaa24";
String regStr = "a1?";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到: "+matcher.group(0) );
}
}
}
非贪婪匹配
package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience12 {
@Test
public void regexpExperience12(){
String content = "hello 1111111 ok";
String regStr = "\\d+?";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到" + matcher.group(0));
}
}
}
4.定位符

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience09 {
@Test
public void regexpExperience09(){
String content = "123abc";
content = "a123abc";
content = "123abc2";
content = "123";
content = "123abc12";
content = "123abc";
content = "123";
content = "123-ab";
content = "hanaiwigahgiahan";
content = "hanaiwigahangiahan";
content = "hanaiwigahan giahan";
content = "hanaiwigahanty giahan";
content = "hanaiwigahanty giahan";
String regStr = "han\\b";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
}
5.分组
常用分组

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience10 {
@Test
public void regexpExperience10(){
String content = "hsp suibian 7788 n3175han";
String regStr = "(\\d\\d)(\\d\\d)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
@Test
public void regexpExperience11(){
String content = "hsp suibian 7788 n3175han";
String regStr = "(\\d\\d)(\\d\\d)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
System.out.println("找到第1个分组" + matcher.group(1));
System.out.println("找到第2个分组" + matcher.group(2));
}
}
@Test
public void regexpExperience12(){
String content = "hsp suibian 7788 n3175han";
String regStr = "(\\d\\d)(\\d)(\\d)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
System.out.println("找到第1个分组" + matcher.group(1));
System.out.println("找到第2个分组" + matcher.group(2));
System.out.println("找到第3个分组" + matcher.group(3));
}
}
@Test
public void regexpExperience13(){
String content = "hsp suibian 7788 n3175han";
String regStr = "(?\\d\\d)(?\\d\\d)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
System.out.println("找到第1个分组" + matcher.group(1));
System.out.println("找到第1个分组(通过组名称来取)" + matcher.group("g1"));
System.out.println("找到第2个分组" + matcher.group(2));
System.out.println("找到第2个分组(通过组名称来取)" + matcher.group("g2"));
}
}
}
非捕获分组

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience11 {
@Test
public void regexpExperience11(){
String content = "hello韩顺平教育 jack韩顺平老师 韩顺平同学hello";
String regStr = "韩顺平(?:教育|老师|同学)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 "+matcher.group(0));
}
}
@Test
public void regexpExperience12(){
String content = "hello韩顺平教育 jack韩顺平老师 韩顺平同学hello";
String regStr = "韩顺平(?=教育|老师)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 "+matcher.group(0));
}
}
@Test
public void regexpExperience13(){
String content = "hello韩顺平教育 jack韩顺平老师 韩顺平同学hello";
String regStr = "韩顺平(?!教育|老师)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 "+matcher.group(0));
}
}
}
正则表达式应用实例

package com.homework;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Homework01 {
@Test
public void homework01(){
String content = "韩顺平教育";
content = "韩顺平a教育";
String regString = "^[\u0391-\uffe5]+$";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
System.out.println("满足格式");
System.out.println(matcher.group(0));
}else {
System.out.println("不满足格式");
}
}
@Test
public void homework02(){
String content = "923890";
String regString = "^[1-9]\\d{5}";
content = "023890";
regString = "^[1-9]\\d{5}";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
@Test
public void homework03(){
String content = "1234567890";
String regString = "^[1-9]\\d{4,9}";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
@Test
public void homework04(){
String content = "13588889999";
String regString = "^1(?:3|4|5|8)\\d{9}$";
regString = "^1(3|4|5|8)\\d{9}$";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
@Test
public void homework05(){
String content = "https://www.bilibili.com/video/BV1fh411y7R8?p=894&spm_id_from=pageDriver&vd_source=aa478ca7c2fa47ecbea3237471050046";
String regString = "^(http(?:|s)://)((\\w)+\\.)+((\\w)+\\/)+((\\w)+(\\?)\\w)(\\=(\\w)+\\&(\\w)+)*(\\=\\w+)";
content = "http://edu.3dsmax.tech/yg/bilibili/my6652/pc/qg/05-51/index.html#201211-1?track_id=jMc0jn-hm-yHrNfVad37YdhOUh41XYmjlss9zocM26gspY5ArwWuxb4wYWpmh2Q7GzR7doU0wLkViEhUlO1qNtukyAgake2jG1bTd23lR57XzV83E9bAXWkStcAh4j9Dz7a87ThGlqgdCZ2zpQy33a0SVNMfmJLSNnDzJ71TU68Rc-3PKE7VA3kYzjk4RrKU";
content = "bilibili.com/video/BV1fh411y7R8?p=894&spm_id_from=pageDriver&vd_source=aa478ca7c2fa47ecbea3237471050046";
regString = "^(http(?:|s)://)?((\\w+\\.)+(\\w+))?(\\/[(\\w+)-?=#.&]*)?$";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
@Test
public void homework06(){
String content = "hello. abc 111";
String regString = ".";
regString = "[.]";
regString = "\\.";
regString = "[?]";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
正则表达式三个常用的类
Pattern类 的常用方法matches

package com.pattern.method;
import org.junit.Test;
import java.lang.reflect.ParameterizedType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternMethod {
@Test
public void patternMethod(){
String content = "hello world hi ";
String regStr = "world";
boolean matches = Pattern.matches(regStr, content);
System.out.println(matches);
regStr = "hello.*";
matches = Pattern.matches(regStr, content);
System.out.println(matches);
}
}
Matches类

package com.matches.method;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchesMethod {
@Test
public void matchesMethod(){
String content = "hello edy jack tom smith hello hsp";
String regStr = "hello";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("============");
System.out.println(matcher.start());
System.out.println(matcher.end());
System.out.println(content.substring(matcher.start(),matcher.end()));
}
boolean matches = matcher.matches();
System.out.println("整体匹配"+matches);
regStr = "hello.*";
pattern = Pattern.compile(regStr);
matcher = pattern.matcher(content);
matches = matcher.matches();
System.out.println("整体匹配2"+matches);
}
}

package com.matches.method;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchesMethod02 {
@Test
public void matchesMethod02(){
String content = "hspeduayghoawghspeduoweg93993hspedu523257235hspedu993";
String regStr = "hspedu";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
String newContent = matcher.replaceAll("韩顺平教育");
System.out.println(newContent);
}
}
分组、捕获、反向引用
提出需求

介绍
案例
package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience13 {
@Test
public void regexpExperience13(){
String content = "33265336aa6634666663446";
String regStr = "(\\d)\\1";
regStr = "(\\d)\\1{4}";
content = "52254444";
regStr = "(\\d)(\\d)\\2\\1";
content = "12321-333999111";
regStr = "\\d{5}-(\\d)\\1{2}(\\d)\\2{2}(\\d)\\3{2}";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 "+matcher.group(0));
}
}
}
经典的结巴程序

package com.regexp;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexpExperience14 {
@Test
public void regexpExperience13(){
String content = "我....我要....学学学学....编程 java!";
String regStr = "\\.";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
String newContent = matcher.replaceAll("");
System.out.println(newContent);
regStr = "(.)\\1+";
pattern = Pattern.compile(regStr);
matcher = pattern.matcher(newContent);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
String result = matcher.replaceAll("$1");
System.out.println(result);
}
@Test
@SuppressWarnings({"all"})
public void regexpExperience14(){
String content = "我....我要....学学学学....编程 java!";
String regStr = "\\.";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
String newContent = matcher.replaceAll("");
System.out.println(newContent);
regStr = "(.)\\1+";
String result = Pattern.compile(regStr).matcher(newContent).replaceAll("$1");
System.out.println(result);
}
}
#String类中使用正则表达式
替换功能

package com.stringreg;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringReg {
@Test
public void stringReg(){
String content = "2000年5月,JDK1.3、JDK1.4和J2SE1.3相继发布," +
"几周后其获得了Apple公司MacOSX的工业标准的支持。2001年9月24日," +
"J2EE1.3发布2002年2月26日,J2SE1.4发布。自此Java的计算能力有了大幅提升";
content = content.replaceAll("JDK1\\.(?:3|4)","JDK");
content = content.replaceAll("JDK1\\.3|JDK1\\.4","JDK");
System.out.println(content);
}
}
判断功能

package com.stringreg;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringReg02 {
@Test
public void stringReg(){
String content = "13811234453";
boolean matches = content.matches("^13(?:8|9)\\d{8}");
System.out.println(matches);
matches = content.matches("13(8|9)\\d{8}");
System.out.println(matches);
}
}
分割功能

package com.stringreg;
import org.junit.Test;
public class StringReg03 {
@Test
public void stringReg(){
String content = "hello#abc-jack12simith~北京";
String[] split = content.split("#|-|~");
for (String s : split) {
System.out.println(s);
}
}
}
课后习题
习题1

package com.homework;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings({"all"})
public class HomeWork02 {
@Test
public void homeWork(){
String content = "[email protected]";
String regString = "^(\\w|-)+@[a-zA-Z(\\.)?]+$";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
boolean matches = content.matches(regString);
System.out.println(matches);
}
}
习题2

package com.homework;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HomeWork03 {
@Test
public void HomeWork(){
String content = "0.1123";
String regString = "^[-+]?(([1-9]\\d*)|0?)(\\.)?(\\d)*$";
Pattern pattern = Pattern.compile(regString);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
boolean matches = content.matches(regString);
System.out.println(matches);
}
}
习题3

package com.homework;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HomeWork04 {
@Test
public void HomeWork(){
String url = "http://www.sohu.com:8080/abc/xxx/yyy/index.html";
url = "http://www.sohu.com:8080/index.html";
url = "http://www.sohu.com:8080/[email protected]";
String regStr = "^(\\w+)://((\\w+\\.?)+):((\\d)+)/(\\w+/+)*(\\w+\\.\\w+)$";
regStr = "^(\\w+)://((\\w+\\.?)+):((\\d)+)/(\\w+/+)*([\\w.~@-]+)$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(url);
while (matcher.find()) {
System.out.println("找到 " + matcher.group(0));
System.out.println("协议 " + matcher.group(1));
System.out.println("域名 "+ matcher.group(2));
System.out.println("端口名 "+ matcher.group(4));
System.out.println("文件名 "+ matcher.group(7));
}
}
}