[Java] regular expressions

Create a regular expression pattern objects
using Pattern.compile () static method to create a Pattern object.

Pattern pattern = Pattern.compile("//w");   //compile的参数是字符串形式的正则表达式

Pattern of the relevant common method

matches ()

After you create a Pattern object using Pattern.compile () static method, then use the Pattern object matcher () method produces a Matcher object. Matcher large class mentioned methods for pattern matching.
Next, we introduce a Matcher object instance find (), group (), start () and end () methods:

public static void main(String[] args) {
        String str = "abcabcabcabc";
        Pattern pattern = Pattern.compile("abc+");   //compile的参数是字符串形式的正则表达式
        Matcher matcher = pattern.matcher(str);     //matcher的参数时要进行匹配的主串
        while (matcher.find()) {
            System.out.println("匹配结果: " + matcher.group() + "起始位置: " + matcher.start() + "结束位置" + (matcher.end() - 1));
        }
    }

find () method to match the entire content in the order in the main string, a match is found result returns true, if not able to match the substring will return false. group () method returns the last successful match substring, if called before a match occurs or called after the match fails will throw an exception, start () and end () method will return a successful match substring index and the index of the starting position of the last position in the main string plus one. Note that the post-match find a successful match always starts matcher1.last () at.

split()

The return value Pattern of split () method produces a String split with the same parameters but use slightly different.
String The split method:
public String [] split (String reglex)
public String [] split (String reglex, int limit)
the Pattern of split method:
public String [] split (String STR)
public String [] split (String STR, int limit)
can see the difference method parameters only in the first two classes string parameter is passed in the form of a string of a regular expression, while the main pattern incoming string pattern matching.

Pattern flag
compile method there is an overloaded method: compile (String reglex, int flag ). This method allows us to specify a flag to using different matching rules. flag values are summarized as follows:

flag values effect
Pattern.CANON_EQ After only two characters to regulate decomposition will match exactly equal. For example: \ u030A will match? , The default mode will not match
Pattern.CASE_INSENSITIVE(?i) Can be used in conjunction with Pattern.UNICODE_CASE ignore case
Pattern.COMMENTS(?x) Spaces are ignored, a string beginning with # are ignored as comments
Pattern.DOTALL(?s) . "" Matches all characters, including line terminators. Not match the default line terminator
Pattern.MULTILINE(?m) End "^" end of the beginning of the end and the beginning of the string and the string of matching lines, "$" matches the line, by default, "^" matches the beginning of the string into the "$" into the matching string
Pattern.UNICODE_CASE(?u) Can be used in conjunction with Pattern.CASE_INSENSITIVE ignore case
Pattern.UNIX_LINES(?d) ".", "^" And "$" into match line terminator
Published 57 original articles · won praise 55 · views 1940

Guess you like

Origin blog.csdn.net/qq_40561126/article/details/104502742