java正则匹配

来源:互联网 发布:ubuntu安装fcitx五笔 编辑:程序博客网 时间:2024/04/30 17:21

在java.util.regex包下,包括如下:

接口 MatchResult

PatternSyntaxException 异常类

枚举类 UnicodeProp 定义了枚举对应特定的操作

ASCII 定义了一些常量

Pattern 正则表达式的编译对象

Matcher 实现了接口 MatchResult

主要使用Pattern, Matcher

1.Pattern

Pattern类没有任何的public constructor,需使用public static Pattern compile(String regex)来新建Pattern实例,其中参数regex为正则表达式。

 /**     * Compiles the given regular expression into a pattern.  </p>     *     * @param  regex     *         The expression to be compiled     *     * @throws  PatternSyntaxException     *          If the expression's syntax is invalid     */    public static Pattern compile(String regex) {        return new Pattern(regex, 0);    }

Pattern pattern = Pattern.compile(“正则表达式”);

分割字符串

pattern.split(CharSequence input),根据正则表达式分隔字符串,并返回String[]

实例:

import java.util.regex.Pattern;public class PatternSplitTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        String regex = "\\.";        String input = "10.13.1.139";        Pattern pattern = Pattern.compile(regex);        String[] splitStr = pattern.split(input);        for(int i = 0; i < splitStr.length; i++){            System.out.println(splitStr[i]);        }    }}

输出:

10131139

2.Matcher

Matcher是正则引擎对象,它也没有任何的public constructor,需使用Pattern实例的public Matcher matcher(CharSequence input)来新建Matcher实例,其中参数input为待匹配的字符串。

 /**     * Creates a matcher that will match the given input against this pattern.     * </p>     *     * @param  input     *         The character sequence to be matched     *     * @return  A new matcher for this pattern     */    public Matcher matcher(CharSequence input) {        if (!compiled) {            synchronized(this) {                if (!compiled)                    compile();            }        }        Matcher m = new Matcher(this, input);        return m;    }

Matcher matcher = pattern.matcher(“字符串”);

Matcher实例的matches()方法判断整个输入字符串是否完全匹配正则表达式,是则返回true, 否则返回false; 将移动下次匹配的位置。

Matcher实例的find()方法是从当前位置开始匹配,找到一个匹配的子串,将移动下次匹配的位置。

Matcher实例的lookingAt()方法总是从第一个字符开始匹配。

Matcher实例的reset()方法给当前Matcher对象配上新的目标,目标是该方法的参数;如果未带参数,则设为原字符串。

Matcher实例的start()返回匹配到的子字符串在字符串中索引位置。

Matcher实例的end()返回匹配到的字符串的最后一个字符的下一个索引位置。

Matcher实例的group()返回匹配到的子字符串。

Matcher实例的replaceAll(String replacement)和repalceFirst(String replacement)用于替换被匹配的字符串。

import java.util.regex.Matcher;import java.util.regex.Pattern;public class PatternTest {    public static void main(String[] args){        String regex = "(\\d{1,3}\\.){3}\\d{1,3}";        String input = "1 10.13.1.139 10.210.230.26";        Pattern pattern = Pattern.compile(regex);        Matcher matcher = pattern.matcher(input);        // 完全匹配为false        System.out.println(matcher.matches());        // 部分匹配为true        System.out.println(matcher.find());        // find()匹配的字符串和起始位置(只有这一次匹配到才能返回)        System.out.println(matcher.group() + "---" + matcher.start() + "---" + + matcher.end());        // 从第一个字符开始匹配,返回false        System.out.println(matcher.lookingAt());        matcher.reset("10.10.10.10");        System.out.println(matcher.find());        System.out.println(matcher.group() + "---" + matcher.start() + "---" + matcher.end());        Pattern pattern2 = Pattern.compile("-");        Matcher matcher2 = pattern2.matcher("10-13-1-139");        System.out.println(matcher2.replaceAll("\\."));        System.out.println(matcher2.replaceFirst("\\."));    }}

输出:

falsetrue10.13.1.139---2---13falsetrue10.10.10.10---0---1110.13.1.13910.13-1-139

匹配中文:

[\u4E00-\u9FA5]

3.捕获分组(Capturing Groups)

在正则表达式中,用’()’新建分组,从左到右中’(‘计算编号,从1开始。输入字符串匹配的不同分组保存在内存中,可以使用反向引用(反斜线 ‘\’),指定的数字代表第几个分组被重新调用。

例如:正则表达式”(A(B(C)))\3”

0 始终代表整个表达式组1 (A(B(C)))组2 (B(C))组3 (C)\\3 重复组3匹配的内容

实例:

import java.util.regex.Matcher;import java.util.regex.Pattern;public class PatternGroupTest {    public static void main(String[] args) {        String regex = "(((\\d{1,3}\\.){3})\\d{1,3})\\1";         String input = "10.13.1.13910.13.1.139";        Pattern pattern = Pattern.compile(regex);        Matcher matcher = pattern.matcher(input);        System.out.println(matcher.matches());        System.out.println(matcher.groupCount());        // 组零始终代表整个表达式        System.out.println(matcher.group(0));        System.out.println(matcher.group(1));        System.out.println(matcher.group(2));        System.out.println(matcher.group(3));           }}

输出:

true310.13.1.13910.13.1.13910.13.1.13910.13.1.1.
0 0
原创粉丝点击