Java Basic Tutorial - Regular Expressions

Regular Expression

Regular expressions are templates for matching strings. Regular expressions are not limited to a particular language, but differ slightly in each language.
The main objects used are:
java.util.regex.Pattern: Compiled representation of regular expressions
java.util.regex.Matcher: Depending on the Patterns object as a matching Pattern, the string is checked for matching

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test01 regular expression {
    public static void main(String[] args) {
        // Pattern: Compiled representation of regular expressions
        Pattern p = Pattern.compile("((13\\d))\\d{8}");// Match the cell phone number at the beginning of 13 ( d: number)
        // Matcher: Depending on the Pattern object as the matching Pattern, the string is checked for matching.
        Matcher m = p.matcher("13200000001,1500000001,13900000002");
        while (m.find()) {
            System.out.print(m.group());
            System.out.print("  Starting position:" + m.start());
            System.out.println("    End position:" + m.end());
        }
    }
}

Operation results:

13200000001 Start Position: 0 End Position: 11
 13900000002 Start Position: 23 End Position: 34

More Grammar

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test02 Various expressions {
    public static void main(String[] args) {
        System.out.println("------Predefined characters (wildcards)------");
        test matching("12abAB One or two $_*#Match any character.
        test matching("12abAB One or two $_*#", \d," (digit) matching number 0-9 ";
        test matching("12abAB One or two $_*#", \D, matching non-numeric";
        test matching("12abAB One or two $_*#"\w," (word) matches word characters, including 0-9, English letters, underscores ";
        test matching("12abAB One or two $_*#",\W," "matching non-word characters"";
        test matching("12[   ]ab\rAB\n One or two\t$_*#", \s," (space) matching blank characters ";
        test matching("12[   ]ab\rAB\n One or two\t$_*#",\S," "matching non-blank characters";
        System.out.println("------Boundary matcher------");
        test matching("never end", "er\\b", "\\b: Matching a word boundary, the position of the word and space");
        test matching("is a verb", "er\\B", "\\B: Matching non-word boundaries");
        test matching("This is", "^(This)", "^Match the beginning");
        test matching("The End.", "(End\\.)$", "$Match End(.It's a predefined character. It needs to be escaped.)");
        System.out.println("------[Use of middle brackets]------");
        test matching("andy,bob,cat,dog", "[abc]", "character set,Match one of the characters");
        test matching("00,01,1a,2c,3e,5f,6g", "[a-f]", "character in range,Matches any character in the specified range");
        test matching("00,01,1a,2c,3e,5f,6g", "[^a-f]", "Reverse range character,Matches any character outside the specified range");
        test matching("12abAB One or two $_*#"[_ 4E00- 9FA5]", matching Chinese ";
        System.out.println("------AND OR------");
        test matching("0123456789", "[1-9&&[^8]]", "&&: And, at the same time, satisfy");
        test matching("0123456789", "[1-7,9]", "comma");
        test matching("food,zood,wood", "(z|f)ood", "|: or");
        System.out.println("------repeat------");
        test matching("z_zo_zoo", "zo+", "One or more matches");
        test matching("z_zo_zoo", "zo{1,}", "+ Equivalent to {1,}");
        test matching("z_zo_zoo", "zo*", "Zero or multiple matches");
        test matching("z_zo_zoo", "zo{0,}", "* Equivalent to {0,}");
        test matching("z_zo_zoo", "zo?", "Zero or one match (multiple matches, no matter)");
        test matching("z_zo_zoo", "zo{0,1}", "? Equivalent to {0,1}");
    }
    static void test matching(String s, String sRegex, String msg) {
        // Pattern: Compiled representation of regular expressions
        Pattern p = Pattern.compile(sRegex);
        // Matcher: Depending on the Pattern object as the matching Pattern, the string is checked for matching.
        Matcher m = p.matcher(s);
        System.out.print("[" + sRegex + "]" + msg + ": ");
        while (m.find()) {
            System.out.print(m.group() + ",");
        }
        System.out.println();
    }
}

Operation results:

------- Predefined characters (wildcards)-------------.
Match any character: 1,2,a,b,A,B, 1, 2, $,, *, #,
[d igit] matching numbers 0-9:1,2,
[D] Matching non-numerals: a,b,A,B, one, two, $,, *, ___,
[w] (word) matches word characters, including 0-9, English letters, underscores: 1,2,a,b,A,B,,
[W] Matches non-word characters: one, two, $, *, #,
[s] (space) matches blank characters:,,
,
,   ,
[S] Matches non-blank characters: 1,2,[,],a,b,A,B, 1, 2, $,,*, _,
The boundary matcher
 [er b] b: Matches a word boundary, that is, the position of the word and space: er,
[er B] B: Matching non-word boundaries: er,
[This] ^ Match the beginning: This,
[End.)$] Matches the end (. is a predefined character that needs to be escaped): End.
-- - [Use of brackets] - ------------------------------------------------------------------------------------------------------------
[a B c]] Character set, matching one of the characters: a,b,b,c,a,
[a-f]] Character range, matching any character in the specified range: a,c,e,f,
[^a-f]] Reverse range character, matching any character beyond the specified range: 0, 0, 0, 1, 1, 2, 3, 5, 6, g,
[1-Er]] Matching Chinese: 1, 2,
------AND OR------
[1-9 & & & [^ 8] &: and, at the same time, satisfy: 1,2,3,4,5,6,7,9,
[1-7,9]: commas 1,2,3,4,5,6,7,9,
[z|f)ood] |: or: food,zood,
Repetition
 [zo+] One or more matches: zo,zoo,
[zo{1,}] + is equivalent to {1,}: zo,zoo,
Zero or multiple matches: z,zo,zoo,
[zo{0,}]* is equivalent to {0,}: z,zo,zoo,
[zo?] Zero or one match (multiple matches, no matter): z,zo,zo,
[zo{0,1}]? Equivalent to {0,1}: z,zo,zo,

Common Verification: Mobile Phone Number, Identity Card Number, Mail Box

package ahjava.p04util.regular_Exp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test03 Common efficacy {
    public static void main(String[] args) {
        String[] sArray = null;
        String sRegEx = null;
        System.out.println("------Mobile phone number validity------");
        // Mobile number segments: 139, 138, 137, 136, 135, 134, 150, 151, 152, 157, 158, 159, 182, 183, 187, 188, 147
        // Unicom Number Section: 130, 131, 132, 136, 185, 186, 145
        // Electric Segment: 133, 153, 180, 189
        sArray = new String[] { "13200000001", "15400000002", "13300000003" };
        sRegEx = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        validate(sArray, sRegEx);
        System.out.println("------Identity card validity------");
        sArray = new String[] { "42002719991231000X", "42002719991231A", "420027199912313",
                "42002719991231004" };
        sRegEx = "(^\\d{15}$)|(^\\d{17}(\\d|X|x)$)";
        validate(sArray, sRegEx);
        System.out.println("------Mailbox validity------");
        sArray = new String[] { "andy@163.com", "bob@qq.com", "cat@hotmail.99" };
        sRegEx = "^\\w+@\\w+.[a-zA-Z]{2,3}(.[a-zA-Z]{2,3})?$";
        validate(sArray, sRegEx);
    }
    static void validate(String[] sArray, String sRegEx) {
        Pattern _pattern = Pattern.compile(sRegEx);
        Matcher matcher = null;
        for (String s : sArray) {
            if (matcher == null) {
                matcher = _pattern.matcher(s);
            } else {
                matcher.reset(s);
            }
            String result = s + (matcher.matches() ? "\t effective" : "\t invalid");
            System.out.println(result);
        }
    }
}

Operation results:

Mobile phone number effect
 13200000001 Effective
 15400000002 is invalid
 13300000003 effective
 The validity of identity card
 42002719991231000X Effective
 42002719991231A is invalid
 420027199912313 is effective
 42002719991231004 is invalid
 Mailbox validity
 andy@163.com is valid
 bob@qq.com is valid
 cat@hotmail.99 is invalid

Keywords: PHP Java Mobile

Added by cashflowtips on Fri, 12 Jul 2019 22:36:31 +0300