天天看點

黑馬程式員_java基礎day25

------- android教育訓練、java教育訓練、期待與您交流! ----------

主要内容:一、正規表達式;二、具體操作功能:1,比對;2,切割;3,替換;4,擷取;三、擷取;四、練習

一、正規表達式:

正規表達式:符合一定規則的表達式。

作用:用于專門操作字元串。

特點:用一些特點的符号來表示一些代碼操作。這樣就簡化書寫。

 是以學習正規表達式,就是在學習一些特殊符号的使用。

好處:可以簡化對字元串的複雜操作。

弊端:符号定義越多,正則越長,閱讀性越差。

二、具體操作功能:1,比對;2,切割;3,替換;4,擷取

    1,比對:String  matches();方法。用規則比對整個字元串,隻要有一處不符合規則,就比對結束,傳回false

    例一:對QQ号碼進行校驗

要求:5~15 0不能開頭 不能是數字

public static void checkQQ()
	{
		String qq = "234561234567890";

		String regex = "[1-9][0-9]{4,14}";

		boolean flag = qq.matches(regex);
		if(flag)
			System.out.println(qq+"...正确");
		else
			System.out.println(qq+"...錯誤");
	}
           

 例二:比對

 手機号段隻有 13xxx 15xxx 18xxx

public static void checkTel()
	{
		
		String tel = "18390000000";
		String telReg = "1[358]\\d{9}";
		boolean flag = tel.matches(telReg);
		
		System.out.println(flag);
	}
           

    2,切割:String  split();方法。

    2.1  按照多個空格來進行切割

String str = "zhangsan    lisi      wangwu";		
	 String reg = " +";
	 String[] arr = str.split(reg);
	 for(String s : arr)
	 {
		System.out.println(s);
	 }
           

    2.2  特殊切割:由于"."代表任意字元,是以需要\\. 表示

         String str = "zhangsan.lisi.wangwu";

String reg = "\\.";

    2.3  特殊切割:

String str = "c:\\abc\\a.txt";

         String reg = "\\\\";

    2.4  按照疊詞完成切割。為了讓規則的結果被重用。

         可以将規則封裝成一個組,用()完成。組的出現都有編号。

從1開始。想要使用已有的組可以通過  \n(n就是組的編号)的形式來擷取。

String str = "erkkuyqqqyiuozzzzzieu";

String reg = "(.)\\1+";

    3,替換:String  replaceAll();方法。

    3.1  将字元串中的數字替換成#

         String str = "wefaf1389435984jklj53663noinn688jlj";

replaceAllDemo(str,"\\d{5,}","#");//替換連續5個以上的數字

    3.2  将疊詞替換成單個字元 zzzz->z

String str1 = "erkkuyqqqyiuozzzzzieu";

         replaceAllDemo(str1,"(.)\\1+","$1");

三、擷取

正規表達式的第四個功能。

4,擷取:将字元串中的符合規則的子串取出。

操作步驟:

1,将正規表達式封裝成對象。

2,讓正則對象和藥操作的字元串相關聯。

3,關聯後,擷取正則比配引擎。

4,通過引擎對符合規則的子串進行操作,比如取出。

    1,類 Pattern

       正規表達式的編譯表示形式。

       沒有構造函數。

    1.1  static Pattern compile(String regex);将給定的正規表達式編譯到模式中。

         例:Pattern p = Pattern.compile(reg);//将規則封裝成對象。

    1.2  Matcher   matcher(CharSequence input);建立比對給定輸入與此模式的比對器。

         例:Matcher m = p.matcher(str);

    1.3  matches();

         在僅使用一次正規表達式時,

可以友善地通過此類定義 matches 方法。此方法編譯表達式并在單個調用中将輸入序列與其比對。

    1.4  find()方法和group方法

先用find找符号的子串,然後用group擷取。

boolean b = m.find();//将規則作用到字元串上,并進行符合規則的子串查找。

System.out.println(m.group());//用于擷取擷取比對後結果。

例:

while(m.find())

{

System.out.println(m.group());

}

例一:

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());//用于擷取擷取比對後結果。
		while(m.find())
		{
			System.out.println(m.group());
			System.out.println(m.start()+"....."+m.end());
		}
	}
}
           

四、幾個練習

練習一: 需求:

将下列字元串轉成:我要學程式設計.

到底用四種功能中的哪一個呢?或者哪幾個呢?

思路方式:

1,如果隻想知道該字元串是對是錯,使用比對。

2,想要将已有的字元串變成另一個字元串,替換。

3,想要按照自定義的方式将字元串變成多個字元串,切割。擷取規則以外的子串。

4,想要拿到符合需求的字元串子串,擷取。擷取符合規則的子串。

public static void test_1()
	{
		String str = "我我...我我...我要..要要...要要...學學學....學學...編編編...程式設計..程.程程...程...程";
		/*
		将已有字元串變成另一個字元串。使用 替換功能。
		1,可以先将 . 去掉。
		2,再将多個重複的内容變成單個内容。
		*/
		str = str.replaceAll("\\.+","");
		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"));
		}
	}
           

練習三:需求:對郵件位址進行校驗。

public static void checkMail()
	{
		String mail = "[email protected]";

		String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//較為精确的比對。
		reg = "\\w+@\\w+(\\.\\w+)+";//相對不太精确的比對。

		System.out.println(mail.matches(reg));
	}
           

練習四:網頁爬蟲(蜘蛛)

import java.io.*;
import java.util.regex.*;
import java.net.*;
class RegexTest2 
{
	public static void main(String[] args) throws Exception
	{
		getMails_1();
	}
	
	/*
	擷取指定網頁上的郵件位址。
	*/
	public static void getMails_1()throws Exception
	{
		URL url = new URL("http://blog.sina.com.cn/s/blog_b5f102950101j65a.html");

		URLConnection conn = url.openConnection();

		BufferedReader bufIn = 
			new BufferedReader(new InputStreamReader(conn.getInputStream()));

		String line = null;

		String mailreg = "\\w+@\\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());
			}
		}
	}
	/*
	擷取指定文檔中的郵件位址。
	使用擷取功能。
	*/
	public static void getMails()throws Exception
	{
		BufferedReader bufr = 
			new BufferedReader(new FileReader("mail.txt"));

		String line = null;

		String mailreg = "\\w+@\\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());
			}
		}
	}
}