Java學習筆記06--Regex

來源:互聯網
上載者:User

標籤:

Regex可以方便的對資料進行匹配,可以執行更加複雜的字串驗證、拆分、替換等操作。

例如:現在要去判斷一個字串是否由數字組成,則可以有以下的兩種做法

不使用Regex
public class T {      public static void main(String[] args) { String str = "0123456789"; boolean flag = true; //定義一個標記變數 char c[] = str.toCharArray(); for (int i = 0; i < c.length; i++) { if(c[i]<‘0‘||c[i]>‘9‘){ flag = false; break; } } if(flag){ System.out.println("是全部由數字組成"); }else{ System.out.println("不是全部由數字組成"); } } }
使用Regex
public class T {      public static void main(String[] args) { String str = "0123456789"; if(Pattern.compile("[0-9]+").matcher(str).matches()){ System.out.println("是全部由數字組成"); }else{ System.out.println("不是全部由數字組成"); } } }

 

Pattern、Matcher類

這兩個類為正則的核心操作類。這兩個類都定義在java.util.regex包中.

Pattern類的主要作用是進行正則規範(如之前的“【0-9】”就屬於正則規範)的編寫,而Matcher類主要是執行規範.

驗證一個字串是否符合其規範。

  • \d:表示數字,【0-9】

  • \D:表示非數字,【^0-9】

  • \w:表示字母、數字、底線,【a-zA-Z0-9】

  • \W:【^a-zA-Z0-9】

以上的正則,如果要想驅動起來,則必須依靠Pattern類和Matcher類

Pattern主要是表示一個規則的意思,即:Regex的規則需要在Pattern類中使用。

Matcher類主要表示使用Pattern指定好的驗證規則。

在Pattern類中如果要想取得Pattern類執行個體,則必須調用compile()方法。

例:驗證一個字串是否是合法的日期格式。

public class T {      public static void main(String[] args) { String str = "1990-04-13"; String pattern = "\\d{4}-\\d{2}-\\d{2}"; Pattern p = Pattern.compile(pattern); //執行個體化Pattern類 Matcher m = p.matcher(str); //執行個體化Matcher類 if(m.matches()){ System.out.println("是合法日期格式"); }else{ System.out.println("不是合法日期格式"); } } }

在Pattern類中也可以使用正則進行字串的拆分功能。

public class T {      public static void main(String[] args) { String str = "AAA000BBB111CCC"; String pattern = "\\d+"; Pattern p = Pattern.compile(pattern); String s[] = p.split(str); for (int i = 0; i < s.length; i++) { System.out.println(s[i]); } } }

還可以使用Matcher類中的字串替換功能。

public class T {      public static void main(String[] args) { String str = "AAA000BBB111CCC"; String pattern = "\\d+"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(str); String s = m.replaceAll("---"); System.out.println(s); //AAA---BBB---CCC } }

 

String類對正則的支援

從之前的操作中,可以返現,很多的代碼除了要求的字串不同,使用的正則規則不同,基本上就沒有什麼特別的了。

所以在JDK1.4之後,java對正則進行了一些擴充,在String中開始直接支援正則的操作。

public class T {      public static void main(String[] args) { String str1 = "AAA000BBB"; String str2 ="1990-04-13"; String str3 = "i_love_you"; System.out.println("字串替換:"+str1.replaceAll("\\d+", "---")); System.out.println("字串驗證:"+str2.matches("\\d{4}-\\d{2}-\\d{2}")); String s[] = str3.split("_"); System.out.println("字串拆分:"); for(int i=0;i<s.length;i++){ System.out.println(s[i]); } } }

 

總結:

1、使用正則可以方便的完成字串的驗證、拆分、替換等複雜功能。

2、在開發中一般都會直接使用String類中提供好的正則支援,而往往很少直接使用Pattern類或Matcher類。

3、在一些正則應用的時候,對於一些敏感的字元要進行轉義操作。

 

 

著作權聲明:本文採用BY-NC-SA協議進行授權,轉載麥田技術部落格的文章請註明出處

原文地址:http://itmyhome.com/2015/03/java-study-notes-regex

Java學習筆記06--Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.