Java Regular Expressions

Source: Internet
Author: User

1, character: matches a single character · A: Match letter A; • \ \: Matches the escape character "\"; \ t: match the escape character "\ T"; \ n: Match the escape character "\ n"; 2, a set of characters: a single character in any match; • [ABC]: May be the letter A, may be the letter B or the letter c; [^ABC]: denotes any one that is not the letter A, the letter B, the letter C; [A-za-z]: denotes any one of the letters; · [0-9]: represents any one of all numbers, 3, boundary matching: the use of regular in the future when writing JavaScript to use; ^: Indicates the beginning of a set of regular; $: Represents the end of a set of regular, 4, shorthand expressions: each of the shorthand tokens that appear is only one; •.: denotes any one character; \d: Represents an arbitrary number, equivalent to "[0-9]"; · \d: Represents any one non-number, equivalent to "[^0-9]"; \w: Denotes any one letter, number, _, equivalent to "[a-za-z0-9_]"; \w: Denotes any one non-letter, number, _, equivalent to "[^a-za-z0-9_]"; \s: Denotes any one space, for example: \ n, \ t, etc.; \s: Represents an arbitrary one non-space; 5, the quantity means: All previous regular all just represent one bit, if want to express multiple bit, then need quantity representation. Regular expression?: This regular appears 0 or 1 times; Regular expression *: This regular appears 0 times, 1 times or more; Regular expression +: This regular appears 1 or more times; Regular expression {n}: This regular appears exactly n times; Regular expression {n,}: This regular appears more than n times; Regular Expression {N,m}: This regular appears n ~ m times. 6, the logical expression: with, or, not · Regular expression A regular expression B: expression A followed by an expression b; Regular expression a| Regular expression B: expression A or expression B, either of them appears; (Regular expression): A representation of multiple sub-expressions is synthesized, appearing as a group.
3.11.3 , String class-to-regular support

After JDK 1.4, the string class has direct method support for the regular, and only a few methods are required to manipulate the regular.

No.

Method name

Type

Describe

1

Public boolean matches (String regex)

Ordinary

Match with specified regular

2

public string ReplaceAll (string regex, string replacement)

Ordinary

Replace all content that satisfies the specified regular

3

public string Replacefirst (string regex, string replacement)

Ordinary

Replace the first content that satisfies the specified regular

4

Public string[] Split (String regex)

Ordinary

Full split by specified regular

5

Public string[] Split (String regex, int limit)

Ordinary

Splits the specified number by the specified regular

Pattern methods that exist within a class:

· Full split of the string: public string[] Split (charsequence input);

· Partial split of string: Public string[] Split (charsequence input, int limit);

Matterh methods that exist within a class:

· String matching: Public boolean matches ();

· Full string substitution: public string replaceall (string replacement);

· The string replaces the first: public string Replacefirst (string replacement).

It is because the string class supports a more comprehensive approach, so in development, the main is the string class operation regular, because it is convenient. There are several operation instances written below for regular validation.

Example: string splitting

Package Cn.mldn.demo;public class Testdemo {public static void main (string[] args) throws Exception {String str = "a1bb2cc C3dddd4eeeee5fffffff6ggggggg7 "; String regex = "[a-za-z]+";//letters appear 1 or more times System.out.println (Str.replaceall (Regex, "")); System.out.println (Str.replacefirst (Regex, ""));}}

Example: string splitting

Package Cn.mldn.demo;public class Testdemo {public static void main (string[] args) throws Exception {String str = "a1bb2cc C3dddd4eeeee5fffffff6ggggggg7 "; String regex = "\\d";//the number appears 1 times, [0-9]string result [] = Str.split (regex); for (int x = 0; x < result.length, x + +) {Syst Em.out.print (Result[x] + ",");}}}

With the regular, the string in the future of the various operations will become quite convenient. The most important part of the process in use is the validation section, because some strings must be satisfied with the specified format before they can be manipulated.

Example: writing a regular expression now requires verifying that the phone number is correct, for example: Now write our phone number, there are several ways:

· One:51283346 \\d{7,8};

· notation Two:01051283346 (\\d{3,4})? \\d{7,8};

· Three:010-51283346 (\\d{3,4})?-? \\d{7,8};

· Four: (010) -51283346 ((\\d{3,4}|\\ (\\d{3,4}\\))-?)? \\d{7,8};

· Five: (010) 51283346 ((\\d{3,4}|\\ (\\d{3,4}\\))? \\d{7,8}.

Package Cn.mldn.demo;public class Testdemo {public static void main (string[] args) throws Exception {String str = "5128334 6 "/////First step: \\d{7,8}, because the phone number may be composed of 7~8 bits;///Second step: (\\d{3,4})? \\d{7,8}, because the area code is composed of four-to-five bits;//Step Three: (\\d{3,4})?-? \\d{7,8}, Because-may or may not appear//Fourth Step: ((\\d{3,4}|\\ (\\d{3,4}\\))-?)? \\d{7,8}string regex = "((\\d{3,4}|\\ (\\d{3,4}\\))-?)? \\d{7,8} "; if (Str.matches (regex)) {//conforms to the authentication requirement System.out.println (" TRUE, the phone number entered is legal. ");} else {System.out.println ("flase, phone number input is illegal! ");}}}

Example: now requires verification of an email address, the email address of the user name is composed of letters, numbers, _,., which can not be the number and. The domain name of the email address can only be. com,. cn,. Net

Package Cn.mldn.demo;public class Testdemo {public static void main (string[] args) throws Exception {String str = "[Email Protected] "; String regex = "[a-za-z_][a-za-z_0-9\\.] *@[a-za-z_0-9\\.] +\\. (com|cn|net) "; if (Str.matches (regex)) {//conforms to the authentication requirements System.out.println (" True,email input is legal. ");} else {System.out.println ("Flase,email input is illegal! ");}}}

The above content excerpt from Li Xinghua java8 Note

Java 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.