Java 正则表达式

  • 一、Pattern 类:
  • 1.1 介绍
  • 1.2 方法目录
  • 1.3 方法详解
  • compile( )方法
  • matches( )方法
  • matcher( )方法
  • 二、Matcher 类:
  • 2.1 介绍
  • 2.2 方法目录
  • 2.3 方法详解
  • matches( )方法
  • lookingAt( )方法
  • matches() 和 lookingAt()
  • 三、PatternSyntaxException:
  • 总结


序言:
正则表达式定义了字符串的模式。
正则表达式可以用来搜索、编辑或处理文本。
正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。
java.util.regex 包主要包括以下三个类:Pattern 类、Matcher 类、PatternSyntaxException

一、Pattern 类:

1.1 介绍

pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。

1.2 方法目录

目录:

java中pattern和matcher java pattern match_java

1.3 方法详解

compile( )方法

public static Pattern compile(String regex)
语法:
Compiles the given regular expression into a pattern.
Parameters:
regex-The expression to be compiled
Returns:
the given regular expression compiled into a pattern

//要与字符串匹配的正则表达式。
String REGEX = "^[qwertyuiopQWERTYUIOP]+$|^[asdfghjklASDFGHJKL]+$|^[zxcvbnmZXCVBNM]+$";
//接受一个正则表达式作为它的第一个参数
Pattern p = Pattern.compile(REGEX);

matches( )方法

public static boolean matches(String regex,CharSequence input)
语法:
Compiles the given regular expression and attempts to match the given input against it.
Parameters:
regex - The expression to be compiled
input - The character sequence to be matched
Returns:
whether or not the regular expression matches on the input
Invocation:
Pattern.compile(regex).matcher(input).matches()
Pattern.matches(regex, input)
String类中同名方法:input_str.matches(regex)

Invocation1:
Pattern.compile(regex).matcher(input_str).matches();
解释:
//创建一个 Pattern 对象,调用其公共静态编译方法,返回一个 Pattern 对象
Pattern p = Pattern.compile(REGEX);
//需要调用Pattern类的pattern对象的 matcher()方法,来获得一个Matcher对象。
Matcher m = p.matcher(input_str);
//Matcher类的m对象调用Matcher类的类方法matches()
m.matches();

Invocation2:
//Pattern类直接调用类方法
Pattern.matches(regex, input)

Invocation3:
//通过String类对象str直接调用类方法
input_str.matches(regex)
matcher( )方法

public Matcher matcher(CharSequence input)
语法:
Creates a matcher that will match the given input against this pattern.
译:创建一个匹配器,将给定的输入与此模式匹配。
Parameters:
input - The character sequence to be matched
Returns:
A new matcher for this pattern

//需要调用 Pattern类的 p对象的 matcher方法来获得一个Matcher对象。
Matcher m = p.matcher(str);

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

public class RegexMatches
{
    private static final String REGEX = "^[asdfghjklASDFGHJKL]+$";
    private static final String INPUT = "Alaska";
    private static final String INPUT2 = "Dad";

    public static void main( String[] args ){
        System.out.println("Current REGEX is: "+REGEX);
        System.out.println("Current INPUT is: "+INPUT);
        System.out.println("Current INPUT2 is: "+INPUT2);

        System.out.println("-------------------------------- ");
        System.out.println("matches()的不同调用方式: ");
        System.out.println("------------匹配INPUT-------------------- ");
        //Invocation1:
        System.out.println("Pattern.compile(REGEX).matcher(INPUT).matches(): "+Pattern.compile(REGEX).matcher(INPUT).matches());
        //Invocation2:
        System.out.println("Pattern.matches(regex, input): "+Pattern.matches(REGEX, INPUT));
        //Invocation3:
        System.out.println("INPUT.matches(REGEX): "+INPUT.matches(REGEX));

        System.out.println("------------匹配INPUT2-------------------- ");
        //Invocation1:
        System.out.println("Pattern.compile(REGEX).matcher(INPUT).matches(): "+Pattern.compile(REGEX).matcher(INPUT2).matches());
        //Invocation2:
        System.out.println("Pattern.matches(regex, input): "+Pattern.matches(REGEX, INPUT2));
        //Invocation3:
        System.out.println("INPUT.matches(REGEX): "+INPUT2.matches(REGEX));
    }
}

***********************************************************
Current REGEX is: ^[asdfghjklASDFGHJKL]+$
Current INPUT is: Alaska
Current INPUT2 is: Dad
-------------------------------- 
matches()的不同调用方式: 
------------匹配INPUT-------------------- 
Pattern.compile(REGEX).matcher(INPUT).matches(): true
Pattern.matches(regex, input): true
INPUT.matches(REGEX): true
------------匹配INPUT2-------------------- 
Pattern.compile(REGEX).matcher(INPUT).matches(): true
Pattern.matches(regex, input): true
INPUT.matches(REGEX): true

>>>>Process finished
***********************************************************

二、Matcher 类:

2.1 介绍

Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。

2.2 方法目录

目录:

java中pattern和matcher java pattern match_System_02

2.3 方法详解

matches( )方法

public boolean matches()
语法:
Attempts to match the entire region against the pattern.
译:试图将整个区域与模式相匹配。
If the match succeeds then more information can be obtained via the start, end, and group methods.
Returns:
true if, and only if, the entire region sequence matches this matcher’s pattern

//创建一个 Pattern 对象,调用其公共静态编译方法,返回一个 Pattern 对象
pattern = Pattern.compile(REGEX);
//需要调用Pattern类的pattern对象的 matcher()方法,来获得一个Matcher对象。
Matcher matcher = pattern.matcher(INPUT);
//通过对象matcher调用类方法matches()
matcher.matches()

lookingAt( )方法

public boolean lookingAt()
语法:
Attempts to match the input sequence, starting at the beginning of the region, against the pattern.
Like the matches method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.
译:不要求整个序列都匹配。
If the match succeeds then more information can be obtained via the start, end, and group methods.
Returns:
true if, and only if, a prefix of the input sequence matches this matcher’s pattern

//创建一个 Pattern 对象,调用其公共静态编译方法,返回一个 Pattern 对象
pattern = Pattern.compile(REGEX);
//需要调用Pattern类的pattern对象的 matcher()方法,来获得一个Matcher对象。
Matcher matcher = pattern.matcher(INPUT);
//通过对象matcher调用类方法lookingAt()
matcher.lookingAt()

matches() 和 lookingAt()

相同:都用来尝试匹配一个输入序列模式,都从序列开头开始匹配。
不同:matches 要求整个序列都匹配,而lookingAt 不要求。

测试用例

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

public class RegexMatches
{
    private static final String REGEX = "foo";
    private static final String INPUT = "fooo";
    private static final String INPUT2 = "ofoo";
    private static Pattern pattern;
    private static Matcher matcher;
    private static Matcher matcher2;

    public static void main( String[] args ){
        pattern = Pattern.compile(REGEX);
        matcher = pattern.matcher(INPUT);
        matcher2 = pattern.matcher(INPUT2);

        System.out.println("Current REGEX is: "+REGEX);
        System.out.println("Current INPUT is: "+INPUT);
        System.out.println("Current INPUT2 is: "+INPUT2);

		//matches 要求整个序列都匹配,而lookingAt 不要求。
        System.out.println("fooo-lookingAt(): "+matcher.lookingAt());
        System.out.println("fooo-matches(): "+matcher.matches());
        //都从序列开头开始匹配
        System.out.println("ofoo-lookingAt(): "+matcher2.lookingAt());
        System.out.println("ofoo-matches(): "+matcher2.matches());
    }
}

***********************************************************
Current REGEX is: foo
Current INPUT is: fooo
Current INPUT2 is: ofoo
fooo-lookingAt(): true
fooo-matches(): false
ofoo-lookingAt(): false
ofoo-matches(): false

>>>>Process finished
***********************************************************

三、PatternSyntaxException:

PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。


总结

String类、Pattern 类、Matcher 类中都有matches( )方法,但调用方式不同,但大致功能一致。