——Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! ——-
1.正規表達式:
符合一定規則的表達式。
作用:用于專門操作字元串。
特點:用于一些特定的符号來表示一些代碼操作。這樣就簡化書寫。
是以學習正規表達式,就是在學習一些特殊符号的使用。
好處:可以簡化對字元串的複雜操作。
弊端:符号定義越多,正則越長,閱讀性越差。
2.具體操作功能:
a,比對:String matches方法。用規則比對整個字元串,隻要有一處不符合規則,就比對結束,傳回false。
b,切割:String split();
c,替換:String replaceAll(regex,str);如果regex中有定義組,可以在第二參數中通過$符号擷取正規表達式中的已有的組。
[java] view plaincopy
class RegexDemo
{
public static void main(String[] args)
{
//demo();
//System.out.println((char)11);
//checkTel();
//splitDemo("zhangsan.lisi.wangwu","\\.");
//splitDemo("c:\\abc\\a.txt","\\\\");
//splitDemo("erkktyqqquizzzzzo","(.)\\1+");//按照疊詞完成切割。為了可以讓規則的結果被重用
//可以将規則封裝成一個組。用()完成。組的出現都有編号。
//從1開始。 想要使用已有的組可以通過 \n(n就是組的編号)的形式來擷取。
String str = "wer1389980000ty1234564uiod234345675f";//将字元串中的數組替換成#。
//replaceAllDemo(str,"\\d{5,}","#");
String str1 = "erkktyqqquizzzzzo";//将疊詞替換成$. //将重疊的字元替換成單個字母。zzzz->z
replaceAllDemo(str1,"(.)\\1+","$1");
}
public static void replaceAllDemo(String str,String reg,String newStr)
{
str = str.replaceAll(reg,newStr);
System.out.println(str);
}
public static void splitDemo(String str,String reg)
{
//String reg = " +";//按照多個空格來進行切割
String[] arr = str.split(reg);
System.out.println(arr.length);
for(String s : arr)
{
System.out.println(s);
}
}
/*
比對
手機号段隻有 13xxx 15xxx 18xxxx
*/
public static void checkTel()
{
String tel = "16900001111";
String telReg = "1[358]\\d{9}";
System.out.println(tel.matches(telReg));
}
public static void demo()
{
String str = "b23a23456789";
String reg = "[a-zA-Z]\\d*";
boolean b= str.matches(reg);
System.out.println(b);
}
public static void checkQQ()
{
String qq = "123a454";
String regex = "[1-9]\\d{4,14}";
boolean flag = qq.matches(regex);
if(flag)
System.out.println(qq+"...is ok");
else
System.out.println(qq+"... 不合法");
}
/*
對QQ号碼進行校驗
要求:5~15 0不能開頭,隻能是數字
這種方式,使用了String類中的方法,進行組合完成了需求。但是代碼過于複雜。
*/
public static void checkQQ_1()
{
String qq = "1882345a0";
int len = qq.length();
if(len>=5 && len<=15)
{
if(!qq.startsWith("0"))<span style="white-space:pre"> </span>//Integer.parseInt("12a");NumberFormatException
{
try
{
long l = Long.parseLong(qq);
System.out.println("qq:"+l);
}
catch (NumberFormatException e)
{
System.out.println("出現非法字元.......");
}
/*
char[] arr = qq.toCharArray();//123a4
boolean flag = true;
for(int x=0;x<arr.length; x++)
{
if(!(arr[x]>='0' && arr[x]<='9'))
{
flag = false;
break;
}
}
if(flag)
{
System.out.println("qq:"+qq);
}
else
{
System.out.println("出現非法字元");
}
*/
}
else
{
System.out.println("不可以0開頭");
}
}
else
{
System.out.println("長度錯誤");
}
}
}
d,擷取:将字元串中的符合規則的子串取出。
操作步驟:
A,将正規表達式封裝成對象。
B,讓正則對象和要操作的字元串相關聯。
C,關聯後,擷取正則比對引擎。
D,通過引擎對符合規則的子串進行操作,比如取出。
[java] view plaincopy
import java.util.regex.*;
class RegexDemo2
{
public static void main(String[] args)
{
getDemo();
}
public static void getDemo()
{
String str = “ming tian jiu yao fang jia le ,da jia。”;
System.out.println(str);
String reg = “\b[a-z]{4}\b”;
//将規則封裝成對象。
Pattern p = Pattern.compile(reg);
//讓正則對象和要作用的字元串相關聯。擷取比對器對象。
Matcher m = p.matcher(str);
//System.out.println(m.matches());//其實String類中的matches方法。用的就是Pattern和Matcher對象來完成的。
//隻不過被String的方法封裝後,用起來較為簡單。但是功能卻單一。
// boolean b = m.find();//将規則作用到字元串上,并進行符合規則的子串查找。
// System.out.println(b);
// System.out.println(m.group());//用于擷取比對後結果。
//System.out.println("matches:"+m.matches());
while(m.find())
{
System.out.println(m.group());
System.out.println(m.start()+"...."+m.end());
}
}
}
3.練習
[java] view plaincopy
import java.util.*;
class RegexTest
{
public static void main(String[] args)
{
// test_1();
// ipSort();
checkMail();
}
public static void checkMail()
{
String mail = “[email protected]”;
mail = "[email protected]";
String reg = "[a-zA-Z0-9_][email protected][a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//較為精确的比對。
reg = "\\[email protected]\\w+(\\.\\w+)+";//相對不太精确的比對。
//mail.indexOf("@")!=-1
System.out.println(mail.matches(reg));
}
/*
需求:
将下列字元串轉成:我要學程式設計.
到底用四種功能中的哪一個呢?或者哪幾個呢?
思路方式:
1,如果隻想知道該字元是否對是錯,使用比對。
2,想要将已有的字元串變成另一個字元串,替換。
3,想要按照自定的方式将字元串變成多個字元串。切割。擷取規則以外的子串。
4,想要拿到符合需求的字元串子串,擷取。擷取符合規則的子串。
*/
public static void test_1()
{
String str = "我我...我我...我要..要要...要要...學學學....學學...編編編...程式設計..程.程程...程...程";
/*
将已有字元串變成另一個字元串。使用 替換功能。
1,可以先将 . 去掉。
2,在将多個重複的内容變成單個内容。
*/
str = str.replaceAll("\\.+","");
System.out.println(str);
str = str.replaceAll("(.)\\1+","$1");
System.out.println(str);
}
/*
192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30
将ip位址進行位址段順序的排序。
還按照字元串自然順序,隻要讓它們每一段都是3位即可。
1,按照每一段需要的最多的0進行補齊,那麼每一段就會至少保證有3位。
2,将每一段隻保留3位。這樣,所有的ip位址都是每一段3位。
*/
public static void ipSort()
{
String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
ip = ip.replaceAll("(\\d+)","00$1");
System.out.println(ip);
ip = ip.replaceAll("0*(\\d{3})","$1");
System.out.println(ip);
String[] arr = ip.split(" ");
TreeSet<String> ts = new TreeSet<String>();
for(String s : arr)
{
ts.add(s);
}
for(String s : ts)
{
System.out.println(s.replaceAll("0*(\\d+)","$1"));
}
}
}
4.網頁爬蟲
[java] view plaincopy
import java.io.*;
import java.util.regex.*;
import java.net.*;
import java.util.*;
class RegexTest2
{
public static void main(String[] args) throws Exception
{
getMails_1();
}
public static void getMails_1()throws Exception
{
URL url = new URL("http://192.168.1.254:8080/myweb/mail.html");
URLConnection conn = url.openConnection();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
String mailreg = "\\[email protected]\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(mailreg);
while((line=bufIn.readLine())!=null)
{
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
}
/*
擷取指定文檔中的郵件位址。
使用擷取功能。Pattern Matcher
*/
public static void getMails()throws Exception
{
BufferedReader bufr = new BufferedReader(new FileReader("mail.txt"));
String line = null;
String mailreg = "\\[email protected]\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(mailreg);
while((line=bufr.readLine())!=null)
{
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
}
}
——Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! ——-