【Java學習筆記】Regex

來源:互聯網
上載者:User

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

1.簡介:

Regex是Java 處理字串、文本的重要工具。

java.util.regex.Pattern   匹配類:用模式比對一個字串所表達的抽象結果。

java.util.regex.Matcher   模式類:用來表示一個編譯過的Regex。

 


2.例子

import java.util.regex.Pattern;

public class RegexDemo1 {
    public static void main(String arg[]) {
        String expr;
        String str;

        expr = "p.t";
        str = "pot";
        boolean b = Pattern.matches(expr,str);//靜態方法,直接調用
        if(b)
            System.out.println(str + " matches pattern " + expr);
        else
            System.out.println(str + " does not match pattern " + expr);
    }
}

 

但是這個例子中的expr每次都要進行編譯才能進行匹配,而下面的例子則不需要。

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexDemo2 {
    public static void main(String arg[]) {
        String expr;
        String str;

        expr = "p//wt";//在Java語言中,//表示要插入Regex的反斜線,且後面的字元有特殊意義。/w 單詞字元 
        str = "pot";
        Pattern pattern = Pattern.compile(expr);
        Matcher matcher = pattern.matcher(str);
        if(matcher.matches())
            System.out.println(str + " matches pattern " + expr);
        else
            System.out.println(str + " does not match pattern " + expr);
    }
}

 

以上這個例子中的pattern就可以重用了,比如:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexDemo3 {
    public static void main(String arg[]) {
        String expr;

        expr = "p[aeiou]t";
        Pattern pattern = Pattern.compile(expr);
        for(int i=0; i<arg.length; i++) {
            Matcher matcher = pattern.matcher(arg[i]);
            if(matcher.matches())
                System.out.println(arg[i] + " matches pattern " + expr);
            else
                System.out.println(arg[i] + " does not match pattern " + expr);
        }
    }
}

 

尋找在一個段落中的匹配地:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexDemo4 {
    public static void main(String arg[]) {
        String expr;
        String str;

        expr = "bark";
        str = "This is a larger block of text the pattern matcher " +
              "will search for the word bark. The pattern is very " +
              "simple. It will only match the word bark, and nothing " +
              "else. You can search a block of text for any of the " +
              "regular expression patterns--not just bark.";
        Pattern pattern = Pattern.compile(expr);
        Matcher matcher = pattern.matcher(str);
        boolean found = matcher.find();
        while(found) {
            int location = matcher.start();
            System.out.println(expr + " found at " + location);
            found = matcher.find(location+1);
       }
    }
}
=================

綜合例子:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

/** 
* Regex例子 
* 
* @author leizhimin 2009-7-17 9:02:53 
*/ 
public class TestRegx { 
        public static void main(String[] args) { 
                Pattern p = Pattern.compile("f(.+?)k"); 
                Matcher m = p.matcher("fckfkkfkf"); 
                while (m.find()) { 
                        String s0 = m.group(); 
                        String s1 = m.group(1); 
                        System.out.println(s0 + "||" + s1); 
                } 
                System.out.println("---------"); 
                m.reset("fucking!"); 
                while (m.find()) { 
                        System.out.println(m.group()); 
                } 

                Pattern p1 = Pattern.compile("f(.+?)i(.+?)h"); 
                Matcher m1 = p1.matcher("finishabigfishfrish"); 
                while (m1.find()) { 
                        String s0 = m1.group(); 
                        String s1 = m1.group(1); 
                        String s2 = m1.group(2); 
                        System.out.println(s0 + "||" + s1 + "||" + s2); 
                } 

                System.out.println("---------"); 
                Pattern p3 = Pattern.compile("(19|20)//d//d([- /.])(0[1-9]|1[012])//2(0[1-9]|[12][0-9]|3[01])"); 
                Matcher m3 = p3.matcher("1900-01-01 2007/08/13 1900.01.01 1900 01 01 1900-01.01 1900 13 01 1900 02 31"); 
                while (m3.find()) { 
                        System.out.println(m3.group()); 
                } 
        } 
}

 輸出結果:fck||c 
fkk||k 
--------- 
fuck 
finish||in||s 
fishfrish||ishfr||s 
--------- 
1900-01-01 
2007/08/13 
1900.01.01 
1900 01 01 
1900 02 31 

Process finished with exit code 0

 

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

聯繫我們

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