Using Basic Regular Expressions in Java

  • 2021-10-15 10:42:24
  • OfStack

1. Introduction to Regular Expressions

Regular expressions are described by a single string that matches a series of strings that match a syntactic rule. Parsing html in crawler can use regularity to extract information conveniently

2. Regular expression matching rules

模式 描述
\w 匹配字母、数字、下划线
\W 匹配字母、数字、下划线
\s 匹配任意空白字符,相当于[\t\n\r\f]
\S 匹配任意非空字符
\d 匹配任意数字,相当于[0-9]
\D 匹配非数字的字符
\A 匹配字符串开头
\Z 匹配字符串结尾,如果存在换行,只匹配到换行前的结束字符串
\z 匹配字符串结尾,如果存在换行,同时还会匹配换行符
\G 匹配最后匹配完成的位置
\n 匹配1个换行符
\t 匹配1个制表符
^ 匹配1行字符串的开头
$ 匹配1行字符串的结尾
. 匹配任意字符,除了换行符
[^…] 不在[]中的字符,比如[^abc]匹配除了a、b、c之外的字符
* 匹配0个或多个表达式
+ 匹配1个或多个表达式
? 匹配0个或1个前面的正则表达式定义的片段,非贪婪方式
() 匹配括号内的表达式,也表示1个组
{n} 精确匹配n个前面的表达式,比如\d{n},代表n个数字
{n,m} 匹配n到m次由前面正则表达式定义的片段,贪婪方式

Code Actual Combat:


public class RegexAction {
    public static void main(String[] args) {
        String s = "Hello 123 4567 World_This is a Regex Demo";
        //match_1(s);
        //match_2(s);
        //match_3(s);
        //match_4(s);
        //match_5(s);
        match_6(s);
    }

    private static void match_1(String s) {
        Pattern pattern = Pattern.compile("^Hello\\s\\d\\d\\d\\s\\d{4}\\s\\w{10}");
        Matcher matcher = pattern.matcher(s);

        if(matcher.find()) {
            System.out.println(matcher.group(0));
        }
    }

    private static void match_2(String s) {
        Pattern pattern = Pattern.compile("Hello\\s(\\d+)\\s\\d{4}\\s\\w{10}");
        Matcher matcher = pattern.matcher(s);

        if(matcher.find()) {
            System.out.println(matcher.group(0));  // The whole result matched 
            System.out.println(matcher.group(1));  // Matched number 1 Results in brackets 
        }
    }

    private static void match_3(String s) {
        Pattern pattern = Pattern.compile("Hello\\s(\\d*)\\s\\d{4}\\s\\w{10}");
        Matcher matcher = pattern.matcher(s);

        if(matcher.find()) {
            System.out.println(matcher.group(0));  // The whole result matched 
            System.out.println(matcher.group(1));  // Matched number 1 Results in brackets 
        }
    }

    private static void match_4(String s) {
        Pattern pattern = Pattern.compile("Hello.*Demo");
        Matcher matcher = pattern.matcher(s);

        if(matcher.find()) {
            System.out.println(matcher.group(0));  // The whole result matched 
        }
    }

    /**
     *  Greedy matching 
     *  Match the middle number, and you can only get 7
     * .* Will match as much data as possible 
     * @param s
     */
    private static void match_5(String s) {
        Pattern pattern = Pattern.compile("Hello.*(\\d+).*Demo");
        Matcher matcher = pattern.matcher(s);

        if(matcher.find()) {
            System.out.println(matcher.group(1));  // The whole result matched 

        }

    }

    /**
     * .*? Non-greedy matching 
     * @param s
     */
    private static void match_6(String s) {
        Pattern pattern = Pattern.compile("Hello.*?(\\d+).*Demo");
        Matcher matcher = pattern.matcher(s);
        if(matcher.find())  {
            System.out.println(matcher.group());
            System.out.println(matcher.group(1));
        }
    }
    
	/**
     *  Regular expression strings can also be used without compiling 
     * @param s
     */
    private static void match_7(String s) {
        String regex = "Hello.*?(\\d+).*Demo";
        boolean flag = s.matches(regex);
        System.out.println(flag);
    }

}
Class Pattern
The pattern object is a compiled representation of a regular expression Class Matcher
The Matcher object is the engine for interpreting and matching input strings find () method
Try to find the next 1 child sequence of the input sequence that matches this pattern until the search ends with the input sequence
find (int start) can be matched from the specified position

Related articles: