javaRegex

來源:互聯網
上載者:User

標籤:編寫   tde   字母   部分   數字   使用   public   nqa   單個字元   

1、字元:匹配單個字元· a:表示匹配字母a;· \\:匹配逸出字元“\”;· \t:匹配逸出字元“\t”;· \n:匹配逸出字元“\n”;2、一組字元:任意匹配裡面的一個單個字元;· [abc]:表示可能是字母a,可能是字母b或者是字母c;· [^abc]:表示不是字母a、字母b、字母c的任意一個;· [a-zA-Z]:表示全部字母中的任意一個;· [0-9]:表示全部數位任意一個;3、邊界匹配:在以後編寫JavaScript的時候使用正則中要使用到;· ^:表示一組正則的開始;· $:表示一組正則的結束;4、簡寫運算式:每一位出現的簡寫標記也只表示一位;· .:表示任意的一位字元;· \d:表示任意的一位元字,等價於“[0-9]”;· \D:表示任意的一位非數字,等價於“[^0-9]”;· \w:表示任意的一位字母、數字、_,等價於“[a-zA-Z0-9_]”;· \W:表示任意的一位非字母、數字、_,等價於“[^a-zA-Z0-9_]”;· \s:表示任意的一位空格,例如:\n、\t等;· \S:表示任意的一位非空格;5、數量表示:之前的所有正則都只是表示一位,如果要想表示多位,則就需要數量表示。· Regex?:此正則出現0次或1次;· Regex*:此正則出現0次、1次或多次;· Regex+:此正則出現1次或多次;· Regex{n}:此正則出現正好n次;· Regex{n,}:此正則出現n次以上;· Regex{n,m}:此正則出現n ~ m次。6、邏輯表示:與、或、非· RegexARegexB:運算式A之後緊跟著運算式B;· RegexA|RegexB:表示運算式A或者是運算式B,二者任選一個出現;· (Regex):將多個子運算式合成一個表示,作為一組出現。
3.11.3、String類對正則的支援

在JDK 1.4之後,String類對正則有了直接的方法支援,只需要通過如下的幾個方法就可以操作正則。

No. 

方法名稱

類型

描述

1 

public boolean matches(String regex) 

普通

與指定正則匹配

2 

public String replaceAll(String regex, String replacement) 

普通

替換滿足指定正則的全部內容

3 

public String replaceFirst(String regex, String replacement) 

普通

替換滿足指定正則的首個內容

4 

public String[] split(String regex) 

普通

按照指定正則全拆分

5 

public String[] split(String regex, int limit) 

普通

按照指定的正則拆分為指定個數

Pattern類之中存在的方法:

· 字串全拆分:public String[] split(CharSequence input);

· 字串部分拆分:public String[] split(CharSequence input, int limit);

Matterh類之中存在的方法:

· 字串匹配:public boolean matches();

· 字串全替換:public String replaceAll(String replacement);

· 字串替換首個:public String replaceFirst(String replacement)。

正是因為String類支援的方法比較全面,所以在開發之中,主要的都是String類操作正則,因為方便。下面編寫若干個操作執行個體來進行正則的驗證。

範例:字串拆分

package cn.mldn.demo;public class TestDemo {public static void main(String[] args) throws Exception {String str = "a1bb2ccc3dddd4eeeee5fffffff6ggggggg7" ;String regex = "[a-zA-Z]+" ;// 字母出現1次或多次System.out.println(str.replaceAll(regex, ""));System.out.println(str.replaceFirst(regex, ""));}}

 

範例:字串拆分

package cn.mldn.demo;public class TestDemo {public static void main(String[] args) throws Exception {String str = "a1bb2ccc3dddd4eeeee5fffffff6ggggggg7" ;String regex = "\\d" ;// 數字出現1次,[0-9]String result [] = str.split(regex) ;for (int x = 0; x < result.length; x++) {System.out.print(result[x] + "、");}}}

 

有了正則,對於字串日後的種種操作就會變的相當的方便。而正則在使用的過程之中最為重要的部分就是驗證部分,因為一些字串必須滿足於指定的格式才可以操作。

範例:現在編寫一個Regex要求驗證電話號碼是否正確,例如:現在寫上咱們電話號碼,有如下幾種寫法:

· 寫法一:51283346  \\d{7,8};

· 寫法二:01051283346  (\\d{3,4})?\\d{7,8};

· 寫法三:010-51283346  (\\d{3,4})?-?\\d{7,8};

· 寫法四:(010)-51283346  ((\\d{3,4}|\\(\\d{3,4}\\))-?)?\\d{7,8};

· 寫法五:(010)51283346  ((\\d{3,4}|\\(\\d{3,4}\\))-?)?\\d{7,8}。

package cn.mldn.demo;public class TestDemo {public static void main(String[] args) throws Exception {String str = "51283346";// 第一步:\\d{7,8},因為電話號碼可能是7~8位所組成;// 第二步:(\\d{3,4})?\\d{7,8},因為區號由3~4位所組成;// 第三步:(\\d{3,4})?-?\\d{7,8},因為-可能出現也可能不出現// 第四步:((\\d{3,4}|\\(\\d{3,4}\\))-?)?\\d{7,8}String regex = "((\\d{3,4}|\\(\\d{3,4}\\))-?)?\\d{7,8}";if (str.matches(regex)) { // 符合於驗證要求System.out.println("TRUE,電話號碼輸入合法。");} else {System.out.println("FLASE,電話號碼輸入非法!");}}}

 

範例:現在要求驗證一個email地址,email地址的使用者名稱由字母、數字、_、.所組成,其中不能以數字和.開頭,而且email地址的網域名稱只能是.com、.cn、.net 

package cn.mldn.demo;public class TestDemo {public static void main(String[] args) throws Exception {String str = "[email protected]";String regex = "[a-zA-Z_][a-zA-Z_0-9\\.]*@[a-zA-Z_0-9\\.]+\\.(com|cn|net)";if (str.matches(regex)) { // 符合於驗證要求System.out.println("TRUE,EMAIL輸入合法。");} else {System.out.println("FLASE,EMAIL輸入非法!");}}}

 以上內容摘抄自李興華java8筆記

 

javaRegex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.