java正则表达式

9/6/2015来源:Java教程人气:1639

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 = "yuki@Gmail.com";        Pattern p2 = Pattern.compile("\\w+@\\w+.[a-zA-Z]+");        Matcher m2 = p2.matcher(s2);        System.out.println("m2.matches() = " + m2.matches());    }}

运行结果:

strArr[0] = histrArr[1] =  i am a tonystrArr[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() = falses = 2015-2-27m.replaceFirst("-") = 2015-2/27isPhone = 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 = trueisMatch2 = falseprice.matches("\d+.\d+")true


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


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