一、正則快速入門
1 為什么要學(xué)習(xí)正則表達(dá)式
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 體驗(yàn)正則表達(dá)式的威力,給我們文本處理帶來哪些好處
*/
public class Regexp_ {
public static void main(String[] args) {
//假定,編寫了爬蟲,從百度頁面得到如下文本
String content = "1995年,互聯(lián)網(wǎng)的蓬勃發(fā)展給了Oak機(jī)會。業(yè)界為了使死板、單調(diào)的" +
"靜態(tài)網(wǎng)頁能夠“靈活”起來,急需一種軟件技術(shù)來開發(fā)一種程序,這種程序可以通" +
"過網(wǎng)絡(luò)傳播并且能夠跨平臺運(yùn)行。于是,世界各大IT企業(yè)為此紛紛投入了大量的" +
"人力、物力和財力。這個時候,Sun公司想起了那個被擱置起來很久的Oak,并且" +
"重新審視了那個用軟件編寫的試驗(yàn)平臺,由于它是按照嵌入式系統(tǒng)硬件平臺體系結(jié)" +
"構(gòu)進(jìn)行編寫的,所以非常小,特別適用于網(wǎng)絡(luò)上的傳輸系統(tǒng),而Oak也是一種精簡的" +
"語言,程序非常小,適合在網(wǎng)絡(luò)上傳輸。Sun公司首先推出了可以嵌入網(wǎng)頁并且可以" +
"隨同網(wǎng)頁在網(wǎng)絡(luò)上傳輸?shù)腁pplet(Applet是一種將小程序嵌入到網(wǎng)頁中進(jìn)行執(zhí)行的技術(shù))," +
"并將Oak更名為Java(在申請注冊商標(biāo)時,發(fā)現(xiàn)Oak已經(jīng)被人使用了,再想了一系列" +
"名字之后,最終,使用了提議者在喝一杯Java咖啡時無意提到的Java詞" +
"語)。5月23日,Sun公司在Sun world會議上正式發(fā)" +
"布Java和HotJava瀏覽器。IBM、Apple、DEC、Adobe、HP、Oracle、Netscape和微軟" +
"等各大公司都紛紛停止了自己的相關(guān)開發(fā)項(xiàng)目,競相購買了Java使用許可證,并為自己的產(chǎn)" +
"品開發(fā)了相應(yīng)的Java平臺";
//提取文章中的所有英文單詞
//(1) 傳統(tǒng)方法 使用遍歷方式,代碼量大,效率不高
//(2) 正則表達(dá)式技術(shù)
//1 先創(chuàng)建一個Pattern對象,模式對象,可以理解成就是一個正則表達(dá)式對象
Pattern pattern = Pattern.compile("[a-zA-z]+");
//Pattern pattern = Pattern.compile("[0-9]+");
//Pattern pattern = Pattern.compile("([0-9]+)|([a-zA-Z]+)");
//2 創(chuàng)建一個匹配器對象
//理解 匹配器按照我們的模式去content文本中查找跟匹配
//找到就返回一個true,否則就返回一個false
Matcher matcher = pattern.matcher(content);
//3 開始循環(huán)匹配
while(matcher.find()){
//匹配的內(nèi)容\文本,會放到m,group(0)
System.out.println("找到:"+matcher.group(0));
}
}
}
?2 再提出幾個問題?
3 解決之道-正則表達(dá)式?
?4 正則表達(dá)式基本介紹
?二、正則表達(dá)式底層實(shí)現(xiàn)(重要)
1 實(shí)例分析
- 為讓大家對正則表達(dá)式底層實(shí)現(xiàn)有一個直觀的映象,給大家舉個實(shí)例
- 給你一段字符串(文本),請找出所有四個數(shù)字連在一起的子串, 比如:
- 應(yīng)該找到 1998 1999 3443 9889 ===> 分析底層實(shí)現(xiàn) RegTheory.java
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 分析Java正則表達(dá)式的底層實(shí)現(xiàn)
*/
public class RegTheory {
public static void main(String[] args) {
String content = "1998 年 12 月 8 日,第二代 Java 平臺的企業(yè)版 J2EE 發(fā)布。1999 年 6 月,Sun 公司發(fā)布了" +
"第二代 Java 平臺(簡稱為 Java2)的 3 個版本:J2ME(Java2 Micro Edition,Java2 平臺的微型" +
"版),應(yīng)用于移動、無線及有限資源的環(huán)境;J2SE(Java 2 Standard Edition,Java 2 平臺的" +
"標(biāo)準(zhǔn)版),應(yīng)用于桌面環(huán)境;J2EE(Java 2Enterprise Edition,Java 2 平臺的企業(yè)版),應(yīng)" +
"用 3443 于基于 Java 的應(yīng)用服務(wù)器。Java 2 平臺的發(fā)布,是 Java 發(fā)展過程中最重要的一個" +
"里程碑,標(biāo)志著 Java 的應(yīng)用開始普及 9889 ";
//目標(biāo):匹配所有四個數(shù)字
//說明
//1 \\d表示任意一個數(shù)字
String regStr="(\\d\\d)(\\d\\d)";
//2 創(chuàng)建模式對象(正則表達(dá)式對象)
Pattern pattern=Pattern.compile(regStr);
//3 創(chuàng)建匹配器
//說明:創(chuàng)建匹配器matcher,按照正則表達(dá)式的規(guī)則去匹配
Matcher matcher=pattern.matcher(content);
//4 開始查找
/**
*matcher.find() 完成的任務(wù) (考慮分組)
*
*
*
*
* matcher.find() 完成的任務(wù) (考慮分組)
* * 什么是分組,比如 (\d\d)(\d\d) ,正則表達(dá)式中有() 表示分組,第 1 個()表示第 1 組,第 2 個()表示第 2 組...
* 1. 根據(jù)指定的規(guī)則,定位滿足規(guī)則的子字符串(比如1999)
* 2. 找到后將子字符串的開始索引記錄到matcher對象屬性 int[] groups
* 2.1 groups[0] = 31 , 把該子字符串的結(jié)束的索引+1 的值記錄到 groups[1] = 35 *
* 2.2 記錄 1 組()匹配到的字符串 groups[2] = 0 groups[3] = 2
* 2.3 記錄 2 組()匹配到的字符串 groups[4] = 2 groups[5] = 4
* 2.4.如果有更多的分組.....
* 3. 同時記錄 oldLast 的值為 子字符串的結(jié)束的 索引+1 的值即 35, 即下次執(zhí)行 find 時,
* 就從 35 開始匹配
*
*
*
*
*
* matcher.group(0) 分析
* *
* * 源碼:
* * public String group(int group) {
* * if (first < 0)
* * throw new IllegalStateException("No match found");
* * if (group < 0 || group > groupCount())
* * throw new IndexOutOfBoundsException("No group " + group);
* * if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
* * return null;
* * return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
* * }
* * 1. 根據(jù) groups[0]=31 和 groups[1]=35 的記錄的位置,從 content 開始截取子字符串返回
* * 就是 [31,35) 包含 31 但是不包含索引為 35 的位置
* *
* * 如果再次指向 find 方法.仍然按照上面分析來執(zhí)行
*/
while (matcher.find()){
//小結(jié)
//1. 如果正則表達(dá)式有() 即分組
//2. 取出匹配的字符串規(guī)則如下
//3. group(0) 表示匹配到的子字符串
//4. group(1) 表示匹配到的子字符串的第一組字串
//5. group(2) 表示匹配到的子字符串的第 2 組字串
//6. ... 但是分組的數(shù)不能越界
System.out.println("找到:"+matcher.group(1)+matcher.group(2));
}
}
}
?三、正則表達(dá)式語法
1 基本介紹
2 元字符(Metacharacter)-轉(zhuǎn)義號 \\?
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class RegExp02 {
public static void main(String[] args) {
String content="abc$(a.bc(123( )";
//匹配( => \\(
String regStr="\\(";
//String regStr = "\\.";
//String regStr = "\\d\\d\\d";
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 " + matcher.group(0));
}
}
}
3?元字符-字符匹配符?
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 演示字符匹配符 的使用
*/
public class RegExp03 {
public static void main(String[] args) {
String content = "a11c8abc _ABCy @";
//String regStr="[a-z]";//匹配 a-z 之間任意一個字符
//String regStr = "[A-Z]";//匹配 A-Z 之間任意一個字符
//String regStr = "abc";//匹配 abc 字符串[默認(rèn)區(qū)分大小寫]
//String regStr = "(?i)abc";//匹配 abc 字符串[不區(qū)分大小寫]
//String regStr = "[0-9]";//匹配 0-9 之間任意一個字符
//String regStr = "[^a-z]";//匹配 不在 a-z 之間任意一個字符
//String regStr = "[^0-9]";//匹配 不在 0-9 之間任意一個字符
//String regStr="[abcd]";
//String regStr="[\\D]";//匹配 不在 0-9 的任意一個字符
//String regStr = "\\w";//匹配 大小寫英文字母, 數(shù)字,下劃線
//String regStr = "\\W";//匹配 等價于 [^a-zA-Z0-9_]
//\\s 匹配任何空白字符(空格,制表符等)
//String regStr = "\\s";
//\\S 匹配任何非空白字符 ,和\\s 剛好相反
String regStr = "\\S";
//. 匹配出 \n 之外的所有字符,如果要匹配.本身則需要使用 \\.
//1. 當(dāng)創(chuàng)建 Pattern 對象時,指定 Pattern.CASE_INSENSITIVE, 表示匹配是不區(qū)分字母大小寫.
// Pattern pattern = Pattern.compile(regStr, Pattern.CASE_INSENSITIVE);
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
4 元字符-選擇匹配符
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 選擇匹配符
*/
public class RegExp04 {
public static void main(String[] args) {
String content = "hanshunping 韓 寒冷";
String regStr = "han|韓|寒";
Pattern pattern = Pattern.compile(regStr, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到 " + matcher.group(0));
}
}
}
?5?元字符-限定符
- 用于指定其前面的字符和組合項(xiàng)連續(xù)出現(xiàn)多少次
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class RegExp05 {
public static void main(String[] args) {
String content = "a211111aaaaaahello";
//String regStr="a{3}";// 表示匹配 aaa
//String regStr="\\d{2}";// 表示匹配 兩位的任意數(shù)字字符
//細(xì)節(jié):java 匹配默認(rèn)貪婪匹配,即盡可能匹配數(shù)位多的的
//String regStr = "a{3,4}"; //表示匹配 aaa 或者 aaaa
//String regStr = "1{4,5}"; //表示匹配 1111 或者 11111
//String regStr = "\\d{2,5}"; //匹配 2 位數(shù)或者 3,4,5
//1+
//String regStr = "1+"; //匹配一個 1 或者多個 1
//String regStr = "\\d+"; //匹配一個數(shù)字或者多個數(shù)字
//1*
//String regStr = "1*"; //匹配 0 個 1 或者多個 1
//演示?的使用, 遵守貪婪匹配
String regStr = "a1?"; //匹配 a 或者 a1
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
?6 元字符-定位符
- 定位符, 規(guī)定要匹配的字符串出現(xiàn)的位置,比如在字符串的開始還是在結(jié)束的位置,這個也是相當(dāng)有用的,必須掌握
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 演示定位符的使用
*/
public class RegExp06 {
public static void main(String[] args) {
String content = "hanshunping sphan nnhan";
//String content="123abc";
String regStr="^[0-9]+[a-z]*";//以至少 1 個數(shù)字開頭,后接任意個小寫字母的字符串
//String regStr = "^[0-9]+\\-[a-z]+$"http://以至少 1 個數(shù)字開頭, 必須以至少一個小寫字母結(jié)束
//表示匹配邊界的 han[這里的邊界是指:被匹配的字符串最后, // 也可以是空格的子字符串的后面]
//String regStr = "han\\b";在這里會查找到兩個han
//和\\b 的含義剛剛相反
//String regStr = "han\\B";
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));//123abc
}
}
}
?7?分組
?【捕獲分組】
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class RegExp07 {
public static void main(String[] args) {
String content="hanshunping s7789 nn1189han";
//下面就是非命名分組
//說明
// 1. matcher.group(0) 得到匹配到的字符串
// 2. matcher.group(1) 得到匹配到的字符串的第 1 個分組內(nèi)容
// 3. matcher.group(2) 得到匹配到的字符串的第 2 個分組內(nèi)容
//String regStr = "(\\d\\d)(\\d\\d)";//匹配 4 個數(shù)字的字符串
//命名分組: 即可以給分組取名
String regStr = "(?<g1>\\d\\d)(?<g2>\\d\\d)";//匹配 4 個數(shù)字的字符串
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
while (matcher.find()){
System.out.println("找到=" + matcher.group(0));
System.out.println("第 1 個分組內(nèi)容=" + matcher.group(1));
System.out.println("第 1 個分組內(nèi)容[通過組名]=" + matcher.group("g1"));
System.out.println("第 2 個分組內(nèi)容=" + matcher.group(2));
System.out.println("第 2 個分組內(nèi)容[通過組名]=" + matcher.group("g2"));
}
}
}
【非捕獲分組】
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 演示非捕獲分組, 語法比較奇怪
*/
public class RegExp08 {
public static void main(String[] args) {
String content="hello 林然教育 jack 林然老師 林然同學(xué) hello 林然學(xué)生";
// 找到 林然教育 、林然老師、林然同學(xué) 子字符串
//String regStr = "林然教育|林然老師|林然同學(xué)";
//上面的寫法可以等價非捕獲分組, 注意:不能 matcher.group(1)
String regStr="林然(?:教育|老師|同學(xué))";
//找到 林然 這個關(guān)鍵字,但是要求只是查找林然教育和 林然老師 中包含有的林然
//下面也是非捕獲分組,不能使用 matcher.group(1)
//String regStr = "林然(?=教育|老師)";
//找到 林然 這個關(guān)鍵字,但是要求只是查找 不是 (林然教育 和 林然老師) 中包含有林然
//下面也是非捕獲分組,不能使用 matcher.group(1)
//String regStr = "林然(?!教育|老師)";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到: " + matcher.group(0));
}
}
}
8 應(yīng)用實(shí)例
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class RegExp10 {
public static void main(String[] args) {
String content="林然教育";
//漢字
String regStr = "^[\u0391-\uffe5]+$";//漢字的范圍
// 郵政編碼
// 要求:1.是 1-9 開頭的一個六位數(shù). 比如:123890
//String regStr="^[1-9]\\d{5}$"
// QQ 號碼
// 要求: 是 1-9 開頭的一個(5 位數(shù)-10 位數(shù)) 比如: 12389 , 1345687 , 187698765
//String regStr = "^[1-9]\\d{4,9}$";
// 手機(jī)號碼
// 要求: 必須以 13,14,15,18 開頭的 11 位數(shù) , 比如 13588889999
//String regStr = "^1[3|4|5|8]\\d{9}$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
if(matcher.find()) {
System.out.println("滿足格式");
} else {
System.out.println("不滿足格式");
}
}
}
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 演示正則表達(dá)式的使用
*/
public class RegExp11 {
public static void main(String[] args) {
String content = "https://www.bilibili.com/video/BV1fh411y7R8?from=search&seid=1831060912083761326";
/**
* 思路
* * 1. 先確定 url 的開始部分 https:// | http://
* 2.然后通過 ([\w-]+\.)+[\w-]+ 匹配 www.bilibili.com
* 3. /video/BV1fh411y7R8?from=sear 匹配(\/[\w-?=&/%.#]*)?
*/
String regStr="^((http|https)://)([\\w-]+\\.)+[\\w-]+(\\/[\\w-?=&/%.#]*)?$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
if(matcher.find()) {
System.out.println("滿足格式");
} else {
System.out.println("不滿足格式");
}
//這里如果使用 Pattern 的 matches 整體匹配 比較簡潔
//System.out.println(Pattern.matches(regStr, content));//
}
}
四、正則表達(dá)式三個常用類
1 Pattern?
package com.hspedu.regexp;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 演示 matches 方法,用于整體匹配, 在驗(yàn)證輸入的字符串是否滿足條件使用
*/
public class PatternMethod {
public static void main(String[] args) {
String content = "hello abc hello, 韓順平教育";
//String regStr = "hello";
String regStr = "hello.*";//判斷匹配模式是否等于content的內(nèi)容
boolean matches= Pattern.matches(regStr,content);
System.out.println("整體匹配= " + matches);
}
}
2 Matcher?
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* Matcher 類的常用方法
*/
public class MatcherMethod {
public static void main(String[] args) {
String content = "hello edu jack hspedutom hello smith hello hspedu hspedu";
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()));
}
//整體匹配方法,常用于,去校驗(yàn)?zāi)硞€字符串是否滿足某個規(guī)則
System.out.println("整體匹配=" + matcher.matches());
//完成如果 content 有 hspedu 替換成 韓順平教育
regStr = "hspedu";
pattern = Pattern.compile(regStr);
matcher = pattern.matcher(content);
//注意:返回的字符串才是替換后的字符串 原來的 content 不變化
String newContent = matcher.replaceAll("韓順平教育");
System.out.println("newContent=" + newContent);
System.out.println("content=" + content);
}
}
五、分組、捕獲、反向引用
1 提出需求
2 介紹?
3 看幾個小案例?
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
* 演示反向引用
*/
public class RegExp12 {
public static void main(String[] args) {
String content="hello jack tom11 jack22 yyy xxx 33333 1221";
//String regStr="(\\d)\\1";
//String regStr="(\\d)\\1{4}";
String regStr="(\\d)(\\d)\\2\\1";
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
?4 經(jīng)典的結(jié)巴程序
- 把 類似 : "我....我要....學(xué)學(xué)學(xué)學(xué)....編程 java!";
- 通過正則表達(dá)式 修改成 "我要學(xué)編程 java" RegExp13.java
package com.hspedu.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class RegExp13 {
public static void main(String[] args) {
String content = "我....我要....學(xué)學(xué)學(xué)學(xué)....編程 java!";
//1. 去掉所有的.
Pattern pattern = Pattern.compile("\\.");
Matcher matcher = pattern.matcher(content);
content=matcher.replaceAll("");
System.out.println(content);
// pattern=Pattern.compile("我+");
// matcher=pattern.matcher(content);
// content=matcher.replaceAll("我");
// System.out.println(content);
//
// pattern=Pattern.compile("學(xué)+");
// matcher=pattern.matcher(content);
// content=matcher.replaceAll("學(xué)");
// System.out.println(content);
//2. 去掉重復(fù)的字 我我要學(xué)學(xué)學(xué)學(xué)編程 java!
// 思路
//(1) 使用 (.)\\1+
//(2) 使用 反向引用$1 來替換匹配到的內(nèi)容
pattern=Pattern.compile("(.)\\1+");//分組的捕獲內(nèi)容記錄到$1
matcher=pattern.matcher(content);
while (matcher.find()){
System.out.println("找到=" + matcher.group(0));
}
content = matcher.replaceAll("$1");
System.out.println("content=" + content);
//3. 使用一條語句 去掉重復(fù)的字 我我要學(xué)學(xué)學(xué)學(xué)編程 java!
//content = Pattern.compile("(.)\\1+").matcher(content).replaceAll("$1");
//System.out.println("content=" + content);
}
}
六?String 類中使用正則表達(dá)式
1 替換功能
2 判斷功能?
3 分割功能?
package com.hspedu.regexp;
/**
* @author 林然
* @version 1.0
*/
public class StringReg {
public static void main(String[] args) {
String content = "2000 年 5 月,JDK1.3、JDK1.4 和 J2SE1.3 相繼發(fā)布,幾周后其" +
"獲得了 Apple 公司 Mac OS X 的工業(yè)標(biāo)準(zhǔn)的支持。2001 年 9 月 24 日,J2EE1.3 發(fā)" +
"布。" +
"2002 年 2 月 26 日,J2SE1.4 發(fā)布。自此 Java 的計算能力有了大幅提升";
//使用正則表達(dá)式方式,將 JDK1.3 和 JDK1.4 替換成 JDK
content = content.replaceAll("JDK1\\.3|JDK1\\.4", "JDK");
System.out.println(content);
//要求 驗(yàn)證一個 手機(jī)號, 要求必須是以 138 139 開頭的
content = "13888889999";
if (content.matches("1(38|39)\\d{8}")) {
System.out.println("驗(yàn)證成功");
} else {
System.out.println("驗(yàn)證失敗");
}
//要求按照 # 或者 - 或者 ~ 或者 數(shù)字 來分割
System.out.println("===================");
content = "hello#abc-jack12smith~北京";
String[] split = content.split("#|-|~|\\d+");
for (String s : split) {
System.out.println(s);
}
}
}
七、本章作業(yè)
package com.hspedu.homework;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class Homework01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String content=sc.nextLine();
String regStr="^[\\w-]+@([a-zA-z]+\\.)+[a-zA-Z]+$";
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
if(matcher.matches()){
System.out.println("合法");
}else {
System.out.println("不合法");
}
}
}
?文章來源:http://www.zghlxwxcb.cn/news/detail-811969.html
package com.hspedu.homework;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class Homework02 {
public static void main(String[] args) {
String content="0.1";
String regStr="^[-+]?(([1-9]\\d*)|0)(\\.\\d+)?$";
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
if(matcher.matches()){
System.out.println("匹配成功");
}else {
System.out.println("匹配失敗");
}
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-811969.html
package com.hspedu.homework;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author 林然
* @version 1.0
*/
public class Homework03 {
public static void main(String[] args) {
String content="https://www.sohu.com:8080/abc/index.htm";
String regStr="^([a-zA-Z]+)://([a-zA-Z.]+[a-zA-Z]+):(\\d+)[\\w-/]*/([\\w.]+)$";
Pattern pattern=Pattern.compile(regStr);
Matcher matcher=pattern.matcher(content);
if(matcher.matches()){
System.out.println(matcher.group(0));
System.out.println("協(xié)議:"+matcher.group(1));
System.out.println("域名:"+matcher.group(2));
System.out.println("端口:"+matcher.group(3));
System.out.println("文件名:"+matcher.group(4));
}else {
System.out.println("匹配失敗");
}
}
}
到了這里,關(guān)于0基礎(chǔ)學(xué)java-day27(正則表達(dá)式)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!