Common Java class libraries-Regular Expressions (Pattern class, Matcher class)

Source: Internet
Author: User

[Java] public class RegexDemo01 {public static void main (String args []) {String str = "1234567890"; // This String consists of digits: boolean flag = true; // define a variable to mark. // you must first split the string into character arrays and then judge char c [] = str in sequence. toCharArray (); // converts a string to a character array for (int I = 0; I <c. length; I ++) {// cyclically judge if (c [I] <'0' | c [I]> '9') {// if conditions are met, it indicates that it is not a number flag = false; // make a flag break; // the program will not continue to run down} if (flag) {System. out. println ("is composed of numbers! ");} Else {System. out. println (" is not composed of numbers! ") ;}}; Basic idea: Split the string and compare the strings one by one. However, this is troublesome, and now it is only used to verify whether the string is composed of digits, what if it is more complex? In this case, replace the code with a regular expression and observe the effect. [java] import java. util. regex. pattern; public class RegexDemo02 {public static void main (String args []) {String str = "1234567890"; // This String consists of numbers if (Pattern. compile ("[0-9] + "). matcher (str ). matches () {// use regular System. out. println ("is composed of numbers! ");} Else {System. out. println (" is not composed of numbers! ") ;}}; 2. Regular Expressions above the Pattern and Matcher classes. If you want to drive them, you must rely on the Pattern class and 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. [Java] import java. util. regex. pattern; import java. util. regex. matcher; public class RegexDemo03 {public static void main (String args []) {String str = "1983-07-27 "; // specify a date format String pat = "\ d {4}-\ d {2}-\ d {2 }"; // specify the Regular Expression 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! ") ;}}; You can also use regular expressions to split strings in the Pattern class. [Java] import java. util. regex. pattern; import java. util. regex. matcher; public class RegexDemo04 {public static void main (String args []) {// retrieve the characters in the String, that is, split String str = "A1B22C333D4444E55555F" according to the number "; // specify a String pat = "\ d +"; // specify the Regular Expression Pattern p = Pattern. compile (pat); // instantiate the Pattern class String s [] = p. split (str); // perform the split operation for (int x = 0; x <s. length; x ++) {System. out. print (s [x] + "\ t ");}} }; You can also use the string replacement function in the Matcher class. Example: replace all numbers in the string with "_" [java] import java. util. regex. pattern; import java. util. regex. matcher; public class RegexDemo05 {public static void main (String args []) {// retrieve the characters in the String, that is, split String str = "A1B22C333D4444E55555F" according to the number "; // specify a String pat = "\ d +"; // specify the Regular Expression Pattern p = Pattern. compile (pat); // instantiate the Pattern class Matcher m = p. matcher (str); // instantiate the object String newString = m. replaceAll ("_"); System. out. println (newString) ;}}; 3. The regular expressions supported by the String class are different from the previous operations, basically nothing special. Therefore, after JDK1.4, Java expands the regular expressions and starts to directly support regular expressions in the String. [Java] import java. util. regex. pattern; import java. util. regex. matcher; public class RegexDemo06 {public static void main (String args []) {String str1 = "A1B22C333D4444E55555F ". replaceAll ("\ d +", "_"); boolean temp = "1983-07-27 ". matches ("\ d {4}-\ d {2}-\ d {2}"); String s [] = "a1b22c333d44e55555f ". split ("\ d +"); System. out. println ("string Replacement Operation:" + str1); System. out. println ("string verification:" + temp); Letter e M. out. print ("string Splitting:"); for (int x = 0; x <s. length; x ++) {System. out. print (s [x] + "\ t") ;}}; however, note this when using regular expressions. Now, assume there is a string sharding program: [java] 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 "; // define a String // The format of Splitting:/* LXH --> 98 MLDN --> 90 LI --> 100 */String s [] = info. split ("\ |"); System. out. println ("string Splitting:"); for (int x = 0; x <s. length; x ++) {String s2 [] = s [x]. split (":"); System. out. prin Tln (s2 [0] + "\ t" + s2 [1]) ;}}; if a character string cannot be split according to a specified character, you need to use the "\" escape. When escaping, two "\" represents a "\". 4. Conclusion 1. using regular expressions can easily complete complex functions such as string verification, splitting, and replacement. 2. Regular Expression support provided by the String class is generally used in development, but Pattern or Matcher classes are rarely used directly. 3. When using regular expressions, escape sensitive characters.

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.