java正则表达式

 
 

java正则表达式

正则表达式


 

一,java.util.regex包中提供了两个类来表示对正则表达式的支持

 

1.Matcher,通过解释Pattern对character sequence 执行匹配操作的引擎

public final class Matcher implements MatchResult

 

2.Pattern,正则表达式的编译表示形式

public final class Pattern implements java.io.Serializable

 

代码:

package yuki.regular;

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

public class FirstTest {

    public static void main(String[] args) {
        
        /**
         * Pattern,正则表达式的编译表示形式
         * public final class Pattern implements java.io.Serializable
         */
        String str = "hi! i am a tony; glad to see you!";
        
        String regex = "\\p{Punct}";
        Pattern pattern = Pattern.compile(regex);
        String[] strArr =  pattern.split(str);
        
        for(int i = 0; i < strArr.length; ++i)
            System.out.println("strArr[" + i + "] = " + strArr[i]);
        
        
        /**
         * Matcher,通过解释Pattern对character sequence执行匹配操作
         * public final class Matcher implements MatchResult
         */
        Matcher matcher = pattern.matcher(str);
        System.out.println(matcher.matches() ? "匹配" : "不匹配");
        System.out.println(
                Pattern.compile("\\p{Punct}+").matcher(".,.;").matches() 
                ? "匹配" : "不匹配");
        
        String s2 = "[email protected]";
        Pattern p2 = Pattern.compile("\\w+@\\w+.[a-zA-Z]+");
        Matcher m2 = p2.matcher(s2);
        System.out.println("m2.matches() = " + m2.matches());
    }
}

 

 

运行结果:

strArr[0] = hi
strArr[1] =  i am a tony
strArr[2] =  glad to see you
不匹配
匹配
m2.matches() = true

 

 

二,String类对正则的支持

代码:

package yuki.regular;

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

public class SecondTest {

    public static void main(String[] args) {
        
        /**
         * 匹配替换
         */
        String date = "2015/2/27";
        Pattern p = Pattern.compile("/");
        Matcher m = p.matcher(date);
        String s = m.replaceAll("-");
        System.out.println("m.matches() = " + m.matches());
        System.out.println("s = " + s); 
        System.out.println("m.replaceFirst(\"-\") = " + m.replaceFirst("-"));
        
        
        //匹配电话号码
        String phone = "0755-28792686";
        String regex = "\\d{3,4}-\\d{7,8}";
        boolean isPhone = phone.matches(regex);
        System.out.println("isPhone = " + isPhone);
    }
}

 

运行结果:

m.matches() = false
s = 2015-2-27
m.replaceFirst("-") = 2015-2/27
isPhone = true

 

 

三,常用示例

代码:

package yuki.regular;

public class ThirdTest {

    public static void main(String[] args) {
        
        /**
         * 至少含有字符串数组中的一个
         */
        String s = "123,456,789,012,345";
        String s2 = "123,456,789,013,345";
        String regex = ".*(234|678|012).*";
        boolean isMatch = s.matches(regex);
        boolean isMatch2 = s2.matches(regex);
        System.out.println("isMatch = " + isMatch);
        System.out.println("isMatch2 = " + isMatch2);
        
        
        //匹配金额
        String price = "499.00";
        System.out.println("price.matches(\"\\d+.\\d+\")" + price.matches("\\d+.\\d+"));
    }
}

 

运行结果:

isMatch = true
isMatch2 = false
price.matches("\d+.\d+")true

 


更多参考:
API:java.util.regex
马剑威_JAVA基础_正则表达式

 


点击下方的红色按钮关注我吧!
孔东阳
二〇一五年二月二十七日
http://www.cnblogs.com/kodoyang/

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。