Java Basics-Regular expressions

Source: Internet
Author: User

Getting Started with regular expressions

Str.matches ("\\d+"); Returns true from 1 to multiple digits

What is a regular expression? is a string, a string consisting of a meta-character "pre-defined character" and a normal character and quantifier.

What is the function of regular expressions? Used to implement matching or filtering of strings.

The meta-characters of the regular expression are as follows:

\d Number (digit)

\d Non-digital

\s Spaces (space)

\s Non-whitespace

\w single character: a symbol in an alphanumeric underline (word)

\w non-single-character: a symbol outside of single-character

. An arbitrary character [except \ n \ r]

^regex$ ^ means start $ means end

| Or

The quantifiers are as follows:

+ 1 or more characters

* 0 or more characters

? 0 or 1 characters

{n} exactly n characters

{N,} at least n characters

{n,m} at least N, up to M characters

Character:

Use [] to list the characters that appear

[Ac?_6] represents one of 5 characters

[0-9] represents one of the 0-9,-Indicates the range

So \w is equivalent to [a-za-z0-9_]

Regular applications:

Common methods for the regular correlation of string classes

Boolean matches (regex); match : match strings according to regular requirements

String[] Split (regex); split : Splits a string according to regular requirements

Split Example:

String str= "I love! You? ";

String regex= "[\\s!? \\d]{1,} "; One or more spaces and exclamation marks, question mark, number

String[] S=str.split (regex);

for (String tem:s) {

println (TEM);

}

println ("number" +s.length);

Accumulation:

Non-negative integer [0-9]+

positive integers [1-9]\\d* OR [1-9][0-9]*

After the beginning of the letter, follow 3-6 arbitrary characters [a-za-z]. {3,6} equivalent [A-za-z]{1}. {3,6}

Replace

String newstr = Str.replaceall (string regex,string replament); Replace Regex with Replament

Accumulation:

Or, either the NBA or the CBA:NBA|CBA.

Phone number: [1][3578][0-9]{9}

Email: [Email protected]+[.]. + is wrong [a-za-z0-9]\\w*[@][a-za-z0-9]+[.] [A-za-z] {2,3}

Example:

Java.util.Pattern

Pattern.matches (Regex,email); Equivalent to Email.matches (regex);

Filter

Not to be continued

Java Basics-Regular expressions

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.