The regular expression of Java learning

Source: Internet
Author: User

The Java Regular expression string pattern.

Regular expressions can be used to search, edit, and manipulate text.

Regular expressions are not limited to one language, but are subtle in each language.

There are 3 main classes in the Java.util.regex package:

    • Pattern class:

      The pattern object is a compiled representation of a regular expression. The pattern class has no public constructor method. To create a pattern object, you must first call its public static compilation method, which returns a Pattern object. The method takes a regular expression as its first argument.

    • Matcher class:

      The Matcher object is the engine that interprets and matches the input string. Like the pattern class, Matcher does not have a public construction method. You need to invoke the Matcher method of the Pattern object to get a Matcher object.

    • Patternsyntaxexception:

      Patternsyntaxexception is a non-mandatory exception class that represents a syntax error in a regular expression pattern.

Capturing groups

A capturing group is a method of processing multiple characters as a single unit, which is created by grouping characters within parentheses.

For example, the regular expression (dog) creates a single group that contains "D", "O", and "G".

Capturing groups are numbered by calculating their opening brackets from left to right. For example, in an expression ((A) (B (C))), there are four such groups:

    • ((A) (B (C)))
    • A
    • (B (C))
    • C

You can see how many groupings of an expression by calling the GroupCount method of the Matcher object. The GroupCount method returns an int value that indicates that the Matcher object currently has more than one capturing group.

There is also a special group (group 0), which always represents the entire expression. The group is not included in the return value of GroupCount.

Regular expression syntax: Use symbols to describe writing rules:/Intermediate Write regular expression/|: Delegate or (): priority

Character Description
\

Marks the next character as a special character, text, inverse reference, and octal escape character. For example "n" matches "n", "\ n" matches line break

"\ T" matches tabs, "\ (" Match "("

^ Match the beginning of a/^ve/match with VE
$ The end of a companion/ve$/with ve
\d Any one number
\d Non-numeric character matching. equivalent to [^0-9].
\w Any number or letter
\s Arbitrary string
N Repeat the expression on the left for N-Times.
{m, n} Repeat the expression on the left at least m times, up to N times
{m,} Repeat the expression on the left at least M-times, at most
+ The expression on the left appears at least once, at most, equal to {1,}
* The expression on the left appears at least 0 times, at most, equal to {0,}
The expression on the left appears at least 0 times, up to 1 times, equivalent to {0,1}
[A,b,c] Can only take one of the contents of square brackets [A-z] from a~z to take any one letter, [1-9] from the 1~9 number of any one
. Match any single character except "\ r \ n"
[^XYZ] The reverse character set. Match any characters that are not included
\b Matches a word boundary, which 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 Matches the control character indicated by x . For example, \cm matches a control-m or carriage return character. The value of x must be between A-Z or a-Z. If this is not the case, then the C is assumed to be the "C" character itself.
\f The page break matches. Equivalent to \x0c and \CL.
\ r Matches a carriage return character. Equivalent to \x0d and \cm.
\s Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s Matches any non-whitespace character. equivalent to [^ \f\n\r\t\v].
\w Matches any character, including underscores. Equivalent to "[a-za-z0-9_]".
\w Matches any non-word character. Equivalent to "[^a-za-z0-9_]".
\uN Matches n, where n is a Unicode character represented by a four-bit hexadecimal number. For example, \u00a9 matches the copyright symbol (?).
Method Index method for Matcher class

The index method provides useful index values that exactly indicate where the match is found in the input string:

Serial Number method and Description
1 public int Start ()
Returns the initial index of the previous match.
2 public int start (int group)
Returns the initial index of a subsequence captured by a given group during a previous match operation
3 public int End ()
Returns the offset after the last matching character.
4 public int end (int group)
Returns the offset after the last character of a subsequence captured by a given group during a previous match operation.

Research methods

The research method is used to check the input string and return a Boolean value indicating whether the pattern is found:

Serial Number method and Description
1 public boolean Lookingat ()
Attempts to match the input sequence starting at the beginning of the zone with the pattern.
2 public Boolean find ()
Attempts to find the next subsequence of the input sequence that matches the pattern.
3 public boolean find (int start)
Resets the match and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
4 public Boolean matches ()
Try to match the entire region to the pattern.

The regular expression of Java learning

Related Article

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.