Java regular expression syntax and java Regular Expression

Source: Internet
Author: User
Tags control characters

Java regular expression syntax and java Regular Expression

Java regular expression syntax

Character Description
\ Mark the next character as a special character, text, reverse reference, or octal escape character. For example, "n" matches the character "n "." \ N "matches the line break. Sequence "\" match "\", "(" match "(".
^ Match the start position of the input string. If the Multiline attribute of the RegExp object is set, ^ matches the position after "\ n" or "\ r.
$ Matches the position at the end of the input string. If the Multiline attribute of the RegExp object is set, $ also matches the position before "\ n" or "\ r.
* Matches the previous character or subexpression zero or multiple times. For example, zo * matches "z" and "zoo ". * Is equivalent to {0 ,}.
+ Match the previous character or subexpression one or more times. For example, "zo +" matches "zo" and "zoo", but does not match "z. + Is equivalent to {1 ,}.
? Matches the previous character or subexpression zero or once. For example, "do (es )?" Match "do" in "do" or "does ".? It is equivalent to {0, 1 }.
{N} N is a non-negative integer. Exactly match n times. For example, "o {2}" does not match "o" in "Bob", but does not match "o" in "food.
{N ,} N is a non-negative integer. Match at least n times. For example, "o {2,}" does not match "o" in "Bob", but matches all o in "foooood ." "O {1,}" is equivalent to "o + "." O {0,} "is equivalent to" o *".
{N, m} M and n are non-negative integers, where n <= m. Match at least n times, at most m times. For example, "o {1, 3}" matches the first three o in "fooooood. 'O {0, 1} 'is equivalent to 'o? '. Note: you cannot insert spaces between commas and numbers.
? When this character is followed by any other qualifier (*, + ,? , {N}, {n ,}, {n, m}), the matching mode is "not greedy "." The non-greedy "pattern matches the searched string as short as possible, while the default" greedy "pattern matches the searched string as long as possible. For example, in the string "oooo", "o + ?" Only one "o" is matched, and "o +" is matched with all "o ".
. Match any single character except "\ r \ n. To match any character including "\ r \ n", use a mode such as "[\ s \ S.
(Pattern) Match pattern and capture the matched child expression. Available 0... 9. Search for the captured match from the "match" set of the result. To match the parentheses (), use "(" or ")".
(? : Pattern) A child expression that matches pattern but does not capture the match. That is, it is a non-capturing match and is not stored for future use. This uses the "or" character (
(? = Pattern) Execute a Forward prediction subexpression to search for a string that matches the start point of the string that matches the pattern. It is a non-capture match, that is, it cannot be captured for future use. For example, 'windows (? = 95
(?! Pattern) Execute the subexpression of reverse prediction first search, which matches the search string that is not at the start point of the string that matches pattern. It is a non-capture match, that is, it cannot be captured for future use. For example, 'windows (?! 95
X Y
[Xyz] Character Set. Match any character. For example, "[abc]" matches "a" in "plain ".
[^ Xyz] Reverse character set. Match any character that is not included. For example, "[^ abc]" matches "p", "l", "I", and "n" in "plain ".
[A-z] Character range. Matches any character in the specified range. For example, "[a-z]" matches any lowercase letter in the range of "a" to "z.
[^ A-z] Reverse range character. Matches any character that is not within the specified range. For example, "[^ a-z]" matches any character that is not in the range of "a" to "z.
\ B Match A Word boundary, that is, the position between the word and the space. For example, "er \ B" matches "er" in "never", but does not match "er" in "verb ".
\ B Non-word boundary match ." Er \ B "matches" er "in" verb ", but does not match" er "in" never ".
\ Cx Match the control characters indicated by x. For example, \ cM matches Control-M or carriage return. The value of x must be between the A-Z or a-z. If not, it is assumed that c is the character "c.
\ D Match numeric characters. It is equivalent to [0-9].
\ D Match non-numeric characters. It is equivalent to [^ 0-9].
\ F Match the page feed. It is equivalent to \ x0c and \ cL.
\ N Line feed match. It is equivalent to \ x0a and \ cJ.
\ R Match a carriage return. It is equivalent to \ x0d and \ cM.
\ S Matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v.
\ S Match any non-blank characters. It is equivalent to [^ \ f \ n \ r \ t \ v.
\ T Tab matching. It is equivalent to \ x09 and \ cI.
\ V Vertical tab matching. It is equivalent to \ x0b and \ cK.
\ W Matches any character type, including underscores. Equivalent to "[A-Za-z0-9.
\ W Matches any non-word character. It is equivalent to "[^ A-Za-z0-9.
\ Xn Match n, where n is a hexadecimal escape code. The hexadecimal escape code must be exactly two digits long. For example, "\ x41" matches ""." \ X041 "is equivalent to" \ x04 "&" 1. ASCII code can be used in regular expressions.
\ Num Matches num. Here, num is a positive integer. To capture matched reverse references. For example, "(.) \ 1" matches two consecutive identical characters.
\ N Identifies an octal escape code or a reverse reference. If \ n contains at least n capture subexpressions, then n is a reverse reference. Otherwise, if n is an octal number (0-7), n is an octal escape code.
\ Nm Identifies an octal escape code or a reverse reference. If there are at least one capture sub-expression before \ nm, then nm is a reverse reference. If there are at least n captures before \ nm, n is a reverse reference, followed by a character m. If neither of the preceding conditions exists, \ nm matches the octal value of nm, where n and m are Octal numbers (0-7 ).
\ Nml When n is the octal number (0-3), m and l are the Octal numbers (0-7), match the octal escape code nml.
\ Un Match n, where n is a four-digit hexadecimal Unicode character. For example, \ u00A9 matches the copyright symbol ().

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.