javaWebRegex,

來源:互聯網
上載者:User

javaWebRegex,

對於web來說,字串的處理特別重要,而Regex是對字串處理的利器,在字元過濾,驗證方面都能看到她的身影。

今天需要處理一段json字串,在用String.replaceAll的過程中,遇到了Regex不會寫的尷尬情境。所以還是簡單地補補Regex的知識吧。

先從一個Regex的使用講起。

import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {    public static void main(String[] args) {                //這個Regex表示匹配1254-8888888,125-6966356這樣子的字元        String regex = "\\d{3,4}-\\d{7}";                //初始字串        String str = "agdf/1254-8888888sssdf125-6966356";        String aft = str.replaceAll(regex, "替換");        System.out.println("repalceAll後==="+aft);                Pattern p = Pattern.compile(regex);        Matcher m = p.matcher(str);        //m.find()為true則一直匹配下去        while(m.find()){            //因為Regex中沒有()括弧,所以擷取的群組數為0            System.out.println("擷取的群組數,m.groupCount==="+m.groupCount());                        //m.group()相當於m.group(0),對應著groupCount,即直接匹配,不分組            System.out.println("m.group==="+m.group(0));                    }            }}

運行結果

repalceAll後===agdf/替換sssdf替換m.group===1254-8888888擷取的群組數,m.groupCount===0m.group===125-6966356擷取的群組數,m.groupCount===0

從那個Regex說起, String regex = "\\d{3,4}-\\d{7}";

"\\d"這個是java的轉義,本來的正則應該是“\d”,表示的是匹配0到9的數字,應該也可以寫成這樣[0-9],沒試過。

"\\d{3,4}"表示\d的個數有3到4個,即匹配123,3212,000這些數字,後面的“\\d{7}”同理,匹配8888888等字元。

至於分組這些,看下下面這個demo

import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {    public static void main(String[] args) {                //這個Regex表示匹配1254-8888888,125-6966356這樣子的字元        String regex = "([a-zA-Z]+)(\\d{7})";                //初始字串        String str = "AGdf12548888888sssdf1256966356";                Pattern p = Pattern.compile(regex);        Matcher m = p.matcher(str);        //m.find()為true則一直匹配下去        while(m.find()){            //因為Regex中沒有()括弧,所以擷取的群組數為0            System.out.println("擷取的群組數,m.groupCount==="+m.groupCount());                        //m.group()相當於m.group(0),對應著groupCount,即直接匹配,不分組            System.out.println("m.group(0)==="+m.group(0));            //對應第一個括弧([a-zA-Z]+)            System.out.println("m.group(1)==="+m.group(1));            //對應第二個括弧(\\d{7})            System.out.println("m.group(2)==="+m.group(2));                        System.out.println("=============我是分行符號============");                    }            }}

運行結果

擷取的群組數,m.groupCount===2m.group(0)===AGdf1254888m.group(1)===AGdfm.group(2)===1254888=============我是分行符號============擷取的群組數,m.groupCount===2m.group(0)===sssdf1256966m.group(1)===sssdfm.group(2)===1256966=============我是分行符號============

最後再附帶下常用的Regex字元

字元的取值範圍
1.[abc] : 表示可能是a,可能是b,也可能是c。
2.[^abc]: 表示不是a,b,c中的任意一個
3.[a-zA-Z]: 表示是英文字母
4.[0-9]:表示是數字

簡潔的字元表示
.:匹配任意的字元
\d:表示數字
\D:表示非數字
\s:表示由Null 字元組成,[ \t\n\r\x\f]
\S:表示由非Null 字元組成,[^\s]
\w:表示字母、數字、底線,[a-zA-Z0-9_]
\W:表示不是由字母、數字、底線組成

數量運算式
1.?: 表示出現0次或1次
2.+: 表示出現1次或多次
3.*: 表示出現0次、1次或多次
4.{n}:表示出現n次
5.{n,m}:表示出現n~m次
6.{n,}:表示出現n次或n次以上

邏輯運算式
1.XY: 表示X後面跟著Y,這裡X和Y分別是Regex的一部分
2.X|Y:表示X或Y,比如"food|f"匹配的是foo(d或f),而"(food)|f"匹配的是food或f
3.(X):子運算式,將X看做是一個整體

聯繫我們

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