Java classes commonly used method [] (7) java.util.regex.Pattern, Matcher of




Foreword

I wrote on a regular summary of expression, which is a talk about the use of Java in the Pattern and Matcher classes.

Regular expressions are summarized venue: [regular expression] (0) summary

Outline

Pattern : Regular expression compiler representation.

Mainly used to write a regular expression.
Here Insert Picture Description

public final class Patternextends Object implements Serializable


Matcher engine performs matching operation for the character sequence by interpreting Pattern.

See also know the name, mainly match to match.
Here Insert Picture Description

public final class Matcher extends Object implements MatchResult

Second, the use

1. Field (Pattern class)

int caseInsensitive = Pattern.CASE_INSENSITIVE; // 忽视大小写,使用Pattern的compile(String regex, int flags) 方法时使用。

2. constructor (Pattern Matcher class and classes)

Class and Pattern Matcher class constructor no.

Pattern class has static methods, static methods can be called directly.

Matcher object is created by matcher Pattern class (CharSequence input). You can see below 3.5 Creating matcher

3. The conventional method (Pattern class)

3.1 compiler to create a regular pattern

Given regular expression pattern to compile.

  • compile(String regex)
  • That is, create a regular expression.
private static Pattern c = Pattern.compile("[a-z]+"); // 创建正则表达式编译到模式中
3.2 compiler to create a regular pattern and given flag

Given regular expression pattern to compile a given sign in.

  • compile(String regex, int flags)
  • That is, create a regular expression. At the same time given a logo, such as: ignore case.
private static Pattern c = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE);
String model 3.3

Returns a string representation of the matcher.

  • toString()
  • Returns the string pattern, which is regular.
/** 在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度 */
private static Pattern p = Pattern.compile("[a-z]+");	
public static void main(String[] args) {
    String s = p.toString();
    System.out.println(s); // [a-z]+
}
3.4 string matching

Compile given regular expression and attempts to given input to match.

  • matches(String regex, CharSequence input)
  • It is to give a string of a regular expression, while giving a need to judge whether the matching string. Returns true when the match does not match the returns false
boolean m = Pattern.matches("[a-z]+", "hello");
System.out.println(m); // true
boolean m2 = Pattern.matches("[a-z]+", "123");
System.out.println(m2); // false
3.5 Creating matcher

Creating match the given input against this pattern matcher.

  • matcher(CharSequence input)
  • Create a matcher, and the matcher matches the character of the given need.
/** 在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度 */
private static Pattern p = Pattern.compile("[a-z]+");
public static void main(String[] args) {
    Matcher hello = p.matcher("hello"); // 获取到一个匹配器
}

4. Common methods (Matcher class)

4.1 string matching

Try the entire region and pattern matching.

  • matches()
  • With above 3.1 string matching the same.
    Pattern is just 3.1 calls the static method matches (), and here is calling the method matches Matcher object.
/** 在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度 */
private static Pattern p = Pattern.compile("[a-z]+");
public static void main(String[] args) {
	Matcher matcher = p.matcher("hello");
	boolean m = matcher.matches();
	System.out.println(m); // true

	Matcher matcher2 = p.matcher("hello 123");
	boolean m2 = matcher2.matches();
	System.out.println(m2); // false
}
4.2 Number of capturing group

Returns the number of capturing groups in this matcher mode.

  • group(int group)
  • Regular expressions, regular expressions on the left will start, every emergence of a left parenthesis "(" mind set to do the first one, and so on.
	private static Pattern p = Pattern.compile("(([a-z]).)");
    public static void main(String[] args) {
        Matcher matcher = p.matcher("a-b-c-d-e-");
        int count = matcher.groupCount();
        System.out.println(count);
    }
4.3 regular replacement
  • replaceFirst(String replacement)
  • Alternatively the first matching string
  • replaceAll(String replacement)
  • Replace all matching strings
	private static Pattern p = Pattern.compile("(([a-z]).)");
    public static void main(String[] args) {
        Matcher matcher = p.matcher("a-b-c-d-e-");
        String s = matcher.replaceFirst("z-"); // 替换第一个匹配的字符串
        System.out.println(s); // z-b-c-d-e-
        String all = matcher.replaceAll("z-"); // 替换所有匹配的字符串
        System.out.println(all); // z-z-z-z-z-
    }
Find obtain matching 4.4

Try to find the input sequence matches the pattern of the next sequence.

  • find()
  • Next try to match a string match, is to find a substring string match

Returns the input subsequence matched previous match operation.

  • group()
  • Substring matches before returning, it is to get the substring string match
    /** 在使用正则表达式时,利用好其预编译功能,可以有效加快正则匹配速度 */
    private static Pattern p = Pattern.compile("[a-z].");

    public static void main(String[] args) {
        Matcher matcher = p.matcher("a-b-c-d-e-");
        // 一个个匹配
        boolean f = matcher.find();
        String g = matcher.group();
        System.out.println("是否有匹配的子字符串:"+f+"\n匹配的结果为:"+g);

        // 循环匹配
        while (matcher.find()) {
            String group = matcher.group();
            System.out.println(group);
        }
    }
Published 99 original articles · won praise 105 · Views 9357

Guess you like

Origin blog.csdn.net/weixin_44034328/article/details/104079875