java【基礎】Regex

來源:互聯網
上載者:User

標籤:void   pid   匹配   分割   upload   產生   rgs   字串替換   oob   

1 字串判斷

 java的正則使用的是Pattern以及Matcher來配合使用的。

   如果只是用來判斷輸入的字串是否符合格式,推薦使用Matcher的matches方法。

public static void main(String[] args) {        // TODO Auto-generated method stub        String str = "[email protected]";        String regEx = "[\\w][email protected][\\w]+\\.com";        Pattern pattren = Pattern.compile(regEx);        Matcher matcher =pattren.matcher(str);        System.out.println(matcher.matches());            }

  如果是需要擷取匹配的分組內容。推薦使用find和group

public static void main(String[] args) {        // TODO Auto-generated method stub        String str = "[email protected]";        String regEx = "([\\w]+)@([\\w]+\\.)com";        Pattern pattren = Pattern.compile(regEx);        Matcher matcher =pattren.matcher(str);        System.out.println(matcher.find());//true        System.out.println(matcher.group(1));//service        System.out.println(matcher.group(2));//qq.            }

2字串分割

使用pattren.split

public static void main(String[] args) {// TODO Auto-generated method stubString str = "[email protected]@x";String regEx = "@";Pattern pattren = Pattern.compile(regEx);String[] strs =pattren.split(str);StringBuffer buf = new StringBuffer();buf.append("[");for(int i=0;i<strs.length;i++) {buf.append(strs[i]);if(i<(strs.length-1)) {buf.append(",");}}buf.append("]");System.out.println(buf.toString());//[service,qq.com,x]}

3字串替換

Matcher replaceFirst 找到第一個位置進行替換
replaceAll 替換所有位置
public static void main(String[] args) {        // TODO Auto-generated method stub        String str = "[email protected]@x";        String regEx = "@";        Pattern pattren = Pattern.compile(regEx);        Matcher matcher = pattren.matcher(str);        System.out.println(matcher.replaceFirst(",@"));//service,@[email protected]        System.out.println(matcher.replaceAll("?"));//service?qq.com?x            }

上面的替換似乎完成了我們想要的功能,但是如果我任性點,我只想替換掉最後的一個@那我怎麼辦,java提供了比較自由的替換appendReplacement。

public static void main(String[] args) {        // TODO Auto-generated method stub        String str = "[email protected]@[email protected]";        String regEx = "@";        Pattern pattren = Pattern.compile(regEx);        Matcher matcher = pattren.matcher(str);        //System.out.println(matcher.replaceFirst(",@"));        //System.out.println(matcher.replaceAll("?"));        StringBuffer sb = new StringBuffer();        int index = 0;        while(matcher.find()) {            index++;            if(index==2) {                matcher.appendReplacement(sb, "?");            }        }        matcher.appendTail(sb);        System.out.println(sb.toString());//[email protected][email protected]            }

上面的是指定位置,這裡我們配合分組來實現這樣一個功能 我要給每個數字後面都加一個$符號 比如 12abc3deg623 我要置換為 12$abc3$deg623$

手冊上:

替換字串可能包含到以前匹配期間所捕獲的子序列的引用:$g 每次出現時,都將被 group(g) 的計算結果替換。$ 之後的第一個數始終被視為組引用的一部分。如果後續的數可以形成合法組引用,則將被合并到 g 中。只有數字 ‘0‘ 到 ‘9‘ 被視為組引用的可能組件。例如,如果第二個組匹配字串 "foo",則傳遞替換字串 "$2bar" 將導致 "foobar" 被添加到字串緩衝區。可能將貨幣符號 ($) 作為替換字串中的字面值(通過前面使用一個反斜線 (\$))包括進來。

注意,在替換字串中使用反斜線 (\) 和貨幣符號 ($) 可能導致與作為字面值替換字串時所產生的結果不同。貨幣符號可視為到如上所述已捕獲子序列的引用,反斜線可用於轉義替換字串中的字面值字元。

public static void main(String[] args) {        // TODO Auto-generated method stub        String str = "12abc3deg623";        String regEx = "^([\\d]+)abc([\\d]+)deg([\\d]+)$";        Pattern pattren = Pattern.compile(regEx);        Matcher matcher = pattren.matcher(str);        StringBuffer sb = new StringBuffer();        //System.out.println(matcher.find());        /*System.out.println(matcher.find());        System.out.println(matcher.groupCount());*/        while(matcher.find()) {            matcher.appendReplacement(sb, "$1\\$abc$2\\$deg$3\\$");        }        matcher.appendTail(sb);        System.out.println(sb.toString());//12$abc3$deg623$            }

java【基礎】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.