Java Regular Expressions

Source: Internet
Author: User

Regular expressions
A regular expression (regex) is a string that is used to describe and match a series of strings that conform to a certain syntactic rule
Purpose: Match, cut, replace, get string
Regular expressions are made up of some ordinary characters and some meta-characters.
Common meta characters
^ matches the starting position of the input string
$ matches the end position of the input string
\d matches a numeric character. equivalent to [0-9]
\d matches a non-numeric character. equivalent to [^0-9]
\s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\n\r\t\f]
\s matches any non-whitespace character. equivalent to [^\n\r\t\f]
\w matches any single character that includes an underscore. Equivalent to "[a-za-z0-9 ]"
\w matches any non-single character. Equivalent to "[^a-za-z0-9
]”
. Match any single character except "\ r \ n"
{n} n is a non-negative integer. Match determined N times
{N,} n is a non-negative integer. Match at least N times
{n,m} m and n are non-negative integers, where n<=m. Matches at least n times and matches up to M

    • Matches the preceding subexpression 0 or more times (greater than or equal to 0 times)
    • ? Match the preceding subexpression 0 or one time
    • Match previous subexpression one or more times (greater than or equal to 1 times)

Understanding of "\"
There are three meanings of backslash "\" in Java:

    1. A backslash can be followed by a specific character to form the so-called "escape character". eg: \ n \ t
    2. Used to cancel the meaning of metacharacters, making metacharacters into ordinary characters. eg: "\" stands for "\".
    3. Used to make up the metacharacters in a regular expression.
      Eg: "\d" represents "match a numeric character" in a regular expression.
      Pattern class and Matcher class
      1. Both the pattern class and the Matcher class are defined in the Java.util.regex package.
      2. The object of the pattern class represents the object after the regular expression is compiled; the Matcher class is primarily used to perform validation.
      3. The main methods of the pattern class are:
        public static Pattern compile (String regex);
        Public? Matcher?matcher (charsequence input)
        The main methods of the Matcher class are:
        public boolean matches ();
        String class support for regular expressions
      4. The public boolean matches (string regex) determines whether a string matches a given regular expression.
        1. public string ReplaceAll (string regex,string replacement) string substitution
        2. Public string[] Split (string regex) string split

Java Regular Expressions

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.