[Common Java class libraries] _ notes on Regular Expressions

Source: Internet
Author: User

[Common Java class libraries] _ notes on Regular Expressions

Objectives of this chapter:
Measure the test taker's knowledge about the functions of regular expressions.
Measure the test taker's knowledge about the regular expression matching mode.
Understanding the use of pattern and matcher classes
Measure the test taker's knowledge about the regular expression supported by string.

For example, to determine whether a string is composed of digits, you can use either of the following methods:
1. Do not use regular expressions;
2. Use regular expressions;

Instance 1:

Public class regexdemo01 {public static void main (string [] ARGs) {string STR = "1234567890"; // This string consists of digits: Boolean flag = true; // define an ID variable char [] C = Str. tochararray (); // converts a string to a character array for (INT I = 0; I <C. length; I ++) {If (C [I] <'0' | C [I]> '9') {flag = false; break; // The program will not continue execution.} If (FLAG) {system. out. println ("is composed of numbers! ");} Else {system. Out. println (" is not composed of numbers! ");}}}

Example 2:

Import Java. util. regEx. pattern; public class regexdemo02 {public static void main (string [] ARGs) {string STR = "13456789"; // This string consists of numbers if (pattern. compile ("[0-9] + "). matcher (STR ). matches () {system. out. println ("is composed of numbers! ");} Else {system. Out. println (" is not composed of numbers! ");}}}

It can be found that the regular expression operation code is simpler.

3.2. pattern, matcher class

Both classes are defined in the Java. util. RegEx package.

The main function of the pattern class is to perform regular expressions (for example, the previous "[0-9]" is a regular expression)
The matcher class mainly implements the specification to verify whether a string complies with the specification.

Common regular expression rule 1:

No Specification Description
1 \ indicates the backslash (\) character
2 \ t indicates a tab
3 \ n indicates line feed
4 [ABC] characters A, B, or C
5 [^ ABC] any character except A, B, and C
6 [a-zA-Z0-9] represents composed of numbers and letters
7 \ D indicates a number
8 \ D indicates a non-digit
9 \ W indicates numbers, letters, and underscores
10 \ W indicates non-numbers, letters, and underscores
11 \ s indicates all blank characters (line breaks, spaces, etc)
12 \ s indicates all non-blank characters
13 ^ The Beginning of the line
14 $ end of the row
15. match any character except line breaks

\ D: Number, [0-9]
\ D: Non-number, [^ 0-9]
\ W: letters, numbers, underscores, [a-zA-Z0-9]
\ W: [^ a-zA-Z0-9]

Common regular expression rule 2:

Number representation (X indicates a set of specifications)

No Specification Description
1 x must appear once
2 x? It can appear 0 or 1 time
3 x * can appear 0 or multiple times
4 x + can appear once or multiple times
5 x {n} can appear n times
6 x {n,} can appear more than N times
7 x {n, m} must appear n to m times

Logical operators (X and Y indicate a set of specifications)

No Specification Description
1 XY x followed by Y
2 x | Y x specification or y Specification
3 (x) as a capture group Specification

If you want to drive the following regular expressions, you must rely on the pattern or matcher class.
Pattern mainly indicates the meaning of a rule, that is, regular expression rules must be used in the pattern class.
The matcher class mainly indicates that the validation rules specified by pattern are used.

Common pattern class methods

No method type description
1 public static pattern compile (string RegEx) specifies regular expression rules
2 Public matcher (charsequence input) Normal Return matcher class instance
3 Public String [] Split (charsequence input) normal string split

In the pattern class, if you want to obtain the pattern class instance, you must call the compile () method.

There are no clear constructor methods available for users in this class. If we are sure that such constructor is private, we can directly obtain the instances of this class from the pattern class.
Specify the regular expression for the operation: public static pattern compile (string RegEx)
It can be instantiated for the matcher class: Public matcher (charsequence input)
Split: Public String [] Split (charsequence input)
Review: The split operation also exists in the string.

Common matcher class methods:
If you want to verify that a string complies with the specifications, you can use the matcher class.

No method type description
1 Public Boolean matches normal execution Verification
2 Public String replaceall (string replacement) Replacement of common strings

Example 2: verify whether a string is in a valid date format

Import Java. util. regEx. pattern; import Java. util. regEx. matcher; public class regexdemo03 {public static void main (string ARGs []) {string STR = "1983-07-27 "; string PAT = "\ D {4}-\ D {2}-\ D {2}"; pattern P = pattern. compile (PAT); // instantiate the pattern class matcher M = P. matcher (STR); // instantiate the matcher class if (M. matches () {// verify the match, using the regular system. out. println ("the date format is valid! ");} Else {system. Out. println (" the date format is invalid! ");}}}

In the pattern class, you can also use regular expressions to split strings.

Import Java. util. regEx. pattern; import Java. util. regEx. matcher; public class regexdemo04 {public static void main (string ARGs []) {string STR = "a1b22c333d444e55555f"; string PAT = "\ D + "; // specify the Regular Expression Pattern P = pattern. compile (PAT); string s [] = P. split (STR); // perform the split operation for (INT x = 0; x <S. length; X ++) {system. out. println (s [x] + "\ t ");}}}

You can also use the replacement function in the matcher class.

Example: replace all numbers in the string with "_"

Import Java. util. regEx. pattern; import Java. util. regEx. matcher; public class regexdemo05 {public static void main (string ARGs []) {string STR = "a1b22c333d444e55555f"; string PAT = "\ D + "; // specify the Regular Expression Pattern P = pattern. compile (PAT); matcher M = P. matcher (STR); string newstring = m. replaceall ("_"); system. out. println (newstring );}}

As long as regular expression validation rules are used, various complex strings can be matched.

3.3. Regular Expressions supported by the string class

From the previous operations, we can find that many codes use different regular rules except for the required strings. Basically, there is nothing special.
Therefore, after jdk1.4, Java expands the regular expressions and starts to directly support regular expressions in the string.

String Support for Regular Expressions

The following three methods in the string class support regular expression operations.

No. method type description
1 Public Boolean matches (string RegEx) Regular String Match
2 Public String replaceall (string RegEx, string replacement) Replacement of common strings
3 Public String [] Split (string RegEx) normal string split

Import Java. util. regEx. pattern; import Java. util. regEx. matcher; public class regexdemo06 {public static void main (string [] ARGs) {string STR = "a1b22c333d444e55555f ". replaceall ("\ D +", "_"); Boolean temp = "1982-07-27 ". matches ("\ D {4}-\ D {2}-\ D {2}"); string s [] = "a1b22c333d444e55555f ". split ("\ D +"); system. out. println ("string Replacement Operation:" + Str); system. out. println ("string verification:" + temp); system. out. println ("string Splitting:"); For (INT x = 0; x <S. length; X ++) {system. out. println (s [x] + "\ t ");}}}

However, you need to pay special attention to the regular expression.
Now, let's assume there is a sharding program like the next string.

Import Java. util. regEx. pattern; import Java. util. regEx. matcher; public class regexdemo07 {public static void main (string [] ARGs) {string info = "lxh: 98 | mldn: 90 | Li: 100 "; string s [] = info. split ("\ |"); system. out. println ("string split:"); For (INT x = 0; x <S. length; X ++) {string S2 [] = s [X]. split (":"); system. out. println (s2 [0] + "\ t" + S2 [1]) ;}}

If you find that a string cannot be split according to the specified character, you need to use "\" escape. When escaping, two "\" characters indicate "\".

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.