正則表達式
一、概述
1、 概念:符合一定規則的表達式。
2、 作用:用於專門操作字符串。
3、 特點:用於一些特定的符號來表示一些代碼操作,這樣可以簡化書寫。所以學習正則表達式,就是在學習一些特殊符號的使用。
4、 好處:可以簡化對字符串的複雜操作。
5、 弊端:符合定義越多,正則越長,閱讀性越差。
二、常見符號
說明:X表示字符X或者匹配的規則。
1、字符
x 字符 x
\\ 反斜線字符
\t 製表符 ('\u0009')
\n 新行(換行)符 ('\u000A')
\r 回車符 ('\u000D')
\f 換頁符 ('\u000C')
\a 報警 (bell) 符 ('\u0007')
2、字符類
[abc] a、b或 c(簡單類)
[^abc] 任何字符,除了 a、b或 c(否定)
[a-zA-Z] a到 z或 A 到 Z,兩頭的字母包括在內(範圍)
[a-d[m-p]] a到 d或 m 到 p:[a-dm-p](並集)
[a-z&&[def]] d、e或 f(交集)
[a-z&&[^bc]] a到 z,除了 b和 c:[ad-z](減去)
[a-z&&[^m-p]] a到 z,而非 m到 p:[a-lq-z](減去)
3、預定義字符類
. 任何字符(與行結束符可能匹配也可能不匹配)
\d 數字:[0-9]
\D 非數字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 單詞字符:[a-zA-Z_0-9]
\W 非單詞字符:[^\w]
4、邊界匹配器
^ 行的開頭
$ 行的結尾
\b 單詞邊界
\B 非單詞邊界
\A 輸入的開頭
\G 上一個匹配的結尾
\Z 輸入的結尾,僅用於最後的結束符(如果有的話)
\z 輸入的結尾
5、Greedy數量詞
X? X,一次或一次也沒有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n次
X{n,} X,至少 n次
X{n,m} X,至少 n次,但是不超過 m 次
6、組和捕獲
捕獲組可以通過從左到右計算其開括號來編號。例如,在表達式 ((A)(B(C)))中,存在四個這樣的組:
1 ((A)(B(C)))
2 \A
3 (B(C))
4 (C)
組零始終代表整個表達式。在替換中常用$匹配組的內容。
三、正則表達式具體功能
主要有四種具體功能:匹配、切割、替換和獲取
1、匹配:String類中的booleanmatches(String
regex)方法。用規則匹配整個字符串,只要有一處不符合規則,就匹配結束,返回false。
示例:
-
class MatchesDemo
-
{
-
-
-
-
-
-
public static void qqCheck_1(String qq)
-
{
-
if (!qq.startsWith("0"))
-
{
-
if (qq.length()>=5&&qq.length()<=15)
-
{
-
try
-
{
-
Long l=Long.parseLong(qq);
-
System.out.println(qq);
-
}
-
catch (NumberFormatException e)
-
{
-
System.out.println("包含非法字符!");
-
}
-
}
-
else
-
System.out.println("你輸入的長度非法!");
-
}
-
else
-
System.out.println("沒有0開頭的號碼,請重輸!");
-
}
-
-
-
public static void qqCheck_2(String qq)
-
{
-
String regex="[1-9]\\d{4,14}";
-
if (qq.matches(regex))
-
{
-
System.out.println(qq);
-
}
-
else
-
System.out.println(qq+":是非法的號碼!");
-
-
}
-
-
-
-
-
-
-
-
public static void phoneCheck(String phone)
-
{
-
String regex="1[358]\\d{9}";
-
if (phone.matches(regex))
-
{
-
System.out.println(phone+":::is ok..");
-
}
-
else
-
System.out.println("手機號碼輸入有誤!");
-
}
-
-
-
public static void main(String[] args)
-
{
-
String qq="125696";
-
qqCheck_1(qq);
-
qqCheck_2(qq);
-
-
String phone="13345678910";
-
phoneCheck(phone);
-
}
-
}
2、切割:String類中的String[]split(String
regex)方法。
示例:
-
class SplitDemo
-
{
-
-
public static void main(String[] args)
-
{
-
String regex1="\\.";
-
String regex2=" +";
-
String regex3="(.)\\1+";
-
String[] arr="192.168.1.62".split(regex1);
-
print(arr);
-
-
arr ="wo shi shui 545 21 3".split(regex2);
-
print(arr);
-
-
arr="erkktyqqquizzzzzo".split(regex3);
-
print(arr);
-
}
-
-
-
public static void print(String[] arr)
-
{
-
for (String s : arr)
-
{
-
System.out.println(s);
-
}
-
}
-
}
說明:
按疊詞完成切割:爲了讓規則被重用,可將規則封裝成一個組,用()完成。組的出現都有編號,從1開始。想要使用已有的組可通過\n(n就是組的編號)的形式來獲取。
對於組中所匹配的字符,可以用$n來獲取。$在正則中表示行的結尾,所以出現在正則中不能用來表示組,一般用於替換中。如下面功能中。
3、替換: String replaceAll(String regex,String replacement)方法。
示例:
-
class ReplaceDemo
-
{
-
public static void main(String[] args)
-
{
-
String regex1="\\d{5,}";
-
String regex2="(.)\\1+";
-
-
String s1="erej569831217woshi2659810wosxs12356f";
-
s1=s1.replaceAll(regex1,"#");
-
-
String s2="erkktyqqquizzzzzo";
-
s2=s2.replaceAll(regex2,"$1");
-
-
System.out.println("s1:"+s1);
-
System.out.println("s2:"+s2);
-
}
-
}
4、獲取:將字符串中的符合規則的子串取出。
操作步驟:
1)將正則表達式封裝成對象。
2)讓正則對象和要操作的字符串相關聯。
3)關聯後,獲取正則匹配引擎。
4)通過引擎對符合規則的子串進行操作,比如取出。
示例:
-
import java .util.regex.*;
-
class PatternDemo
-
{
-
public static void main(String[] args)
-
{
-
String s= "ming tian jiu yao fang jia le ,da jia。";
-
String regex="\\b[a-z]{4}\\b";
-
get(s,regex);
-
}
-
public static void get(String s,String regex)
-
{
-
-
Pattern p=Pattern.compile(regex);
-
-
Matcher m=p.matcher(s);
-
-
-
-
-
-
while(m.find())
-
{
-
System.out.println(m.group());
-
System.out.println(m.start()+"...."+m.end());
-
-
}
-
}
-
}
四、練習
四種功能的選擇(思路方式):
1)如果只想知道該字符是否對是錯,使用匹配。
2)想要將已有的字符串變成另一個字符串,替換。
3)想要按照自定的方式將字符串變成多個字符串。切割。獲取規則以外的子串。
4)想要拿到符合需求的字符串子串,獲取。獲取符合規則的子串。
練習1
-
-
-
-
-
-
-
-
-
-
-
-
class ReplaceTest
-
{
-
public static void main(String[] args)
-
{
-
String s="我我...我..我要...要...要要....學學....學學學......編編編...程...程程....";
-
System.out.println(s);
-
-
String regex="\\.+";
-
s=s.replaceAll(regex,"");
-
System.out.println(s);
-
-
regex="(.)\\1+";
-
s=s.replaceAll(regex,"$1");
-
System.out.println(s);
-
}
-
}
練習2
-
-
-
-
-
-
-
-
-
-
-
-
import java.util.*;
-
-
class IPSortTest
-
{
-
public static void main(String[] args)
-
{
-
String ip="192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.301";
-
System.out.println(ip);
-
-
String regex="(\\d+)";
-
ip=ip.replaceAll(regex,"00$1");
-
System.out.println(ip);
-
-
regex="0*(\\d{3})";
-
ip=ip.replaceAll(regex,"$1");
-
System.out.println(ip);
-
-
regex=" ";
-
String[] arr=ip.split(regex);
-
-
-
TreeSet<String > ts=new TreeSet<String>();
-
for (String str : arr )
-
{
-
ts.add(str);
-
}
-
-
regex="0*(\\d)";
-
for (String s : ts)
-
{
-
System.out.println(s.replaceAll(regex,"$1"));
-
}
-
}
-
}
練習3
-
-
-
class CheckMail
-
{
-
public static void main(String[] args)
-
{
-
String mail="[email protected]";
-
String regex="\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";
-
regex="\\w+@\\w+(\\.\\w+)+";
-
-
boolean b=mail.matches(regex);
-
System.out.println(b);
-
}
-
}
練習4
-
-
-
-
-
-
-
-
import java.net.*;
-
import java.util.regex.*;
-
import java.io.*;
-
-
class Spider
-
{
-
public static void main(String[] args)throws Exception
-
{
-
-
getWebMail();
-
-
}
-
-
-
public static void getWebMail()throws Exception
-
{
-
-
URL url=new URL("http://tieba.baidu.com/p/1390896758");
-
-
URLConnection conn=url.openConnection();
-
-
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
-
String line=null;
-
-
-
String regex="\\w+@\\w+(\\.\\w+)+";
-
Pattern p=Pattern.compile(regex);
-
-
while ((line=br.readLine())!=null)
-
{
-
-
Matcher m=p.matcher(line);
-
-
while (m.find())
-
{
-
System.out.println(m.group());
-
}
-
}
-
}
-
-
-
public static void getFileMail()throws Exception
-
{
-
-
File file=new File("E:\\Java Study\\Practice\\day25\\mail.txt");
-
-
BufferedReader br=new BufferedReader(new FileReader(file));
-
String line=null;
-
-
-
String regex="\\w+@[a-zA-Z]+(\\.[a-zA-z]+)+";
-
-
Pattern p=Pattern.compile(regex);
-
-
-
while ((line=br.readLine())!=null)
-
{
-
-
-
Matcher m=p.matcher(line);
-
while (m.find())
-
{
-
System.out.println(m.group());
-
}
-
}
-
}
-
}