This article describes the usage of Java matches, Patterns and matcher classes. Share it for your reference, as follows:
Pattern class
Common rules for regular expressions
A: Characters
x character x. Example: 'a' means character a
\\ Backslash character.
\n New line (line break) character ('\u000A')
\r Carriage return character ('\u000D')
B: Character Class
[abc] a, b or c (simple class), one of
[^abc] Any character except a, b, or c (negative)
[a-zA-Z] a to z or A to Z, letters at both ends are included (range)
[0-9] Characters from 0 to 9 include
C: Predefined character class
. Any character. Mine is. How do you represent the character itself? \.
\d Number: [0-9]
\w Word characters: [a-zA-Z_0-9]
The things that make up words in regular expressions must be composed of these things
D: Boundary Matcher
^ The beginning of the line
The end of the $ line
\b Word boundaries
It's not a word character.
Example: hello world?haha;xi
E:Greedy Quantitative Word
X? X, once or once
X* X, zero or multiple times
X+ X, once or more
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n times, but not more than m times
Common functions of regular expressions
A: Judgment function
String classpublic boolean matches(String regex)
//The rules for defining mobile phone numbers String regex = "1[38]\\d{9}"; //Calling the function, just make a judgmentboolean flag = (regex);
fengqingyang@
//Define the rules for mailboxString regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+"; //Calling the function, just make a judgment boolean flag = (regex);
B:Segmentation function
String classpublic String[] split(String regex)
//Define an age search rangeString ages = "18-24"; //Define the rulesString regex = "-"; //Calling methodString[] strArray = (regex); String s2 = ""; String[] str2Array = ("\\."); //We should replace the path on the hard disk with \\\String s4 = "E:\\JavaSE\\day14\\avi"; String[] str4Array = ("\\\\");
Sorting strings
package cn.itcast_03; import ; /* * I have the following string: "91 27 46 38 50" * Please write code to achieve the final output result: "27 38 46 50 91" * * analyze: * A: Define a string * B: Split the string to get an array of strings * C: Convert string array into int array * D: Sort the int array * E: Assemble the sorted int array into a string * F: Output string */ public class RegexTest { public static void main(String[] args) { // Define a string String s = "91 27 46 38 50"; // Split the string to get an array of strings String[] strArray = (" "); // Convert string array into int array int[] arr = new int[]; for (int x = 0; x < ; x++) { arr[x] = (strArray[x]); } // Sort the int array (arr); // Assemble the sorted int array into a string StringBuilder sb = new StringBuilder(); for (int x = 0; x < ; x++) { (arr[x]).append(" "); } //Convert to string String result = ().trim(); //Output string ("result:"+result); } }
C: Replacement function
String classpublic String replaceAll(String regex,String replacement)
package cn.itcast_04; /* * Replacement function * public String replaceAll(String regex,String replacement) of String class * Replace this string with the given replacement all substrings that match the given regular expression. */ public class RegexDemo { public static void main(String[] args) { // Define a string String s = "helloqq12345worldkh622112345678java"; // Kill the numbers directly String regex = "\\d+"; String ss = ""; String result = (regex, ss); (result); } }
D: Get function
Pattern and Matcher
Pattern p = ("a*b"); Matcher m = ("aaaaab");
find()
: Find whether it exists or notgroup()
: Get the data you just searched for
package cn.itcast_05; import ; import ; /* * Get function * Use of Pattern and Matcher classes * * Basic usage order of patterns and matchers */ public class RegexDemo { public static void main(String[] args) { // Typical order of call for patterns and matchers // Compile regular expressions into pattern objects Pattern p = ("a*b"); // Get the matcher object through the pattern object, what is needed at this time is the matching string Matcher m = ("aaaaab"); // Call the function of matching object boolean b = (); (b); //This is a judgment function, but if you make a judgment, it will be a bit troublesome. We directly use the string method to do it String s = "aaaaab"; String regex = "a*b"; boolean bb = (regex); (bb); } }
package cn.itcast_05; import ; import ; /* * Get function: * Get the word composed of three characters in the following string * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu? */ public class RegexDemo2 { public static void main(String[] args) { // Define string String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?"; // rule String regex = "\\b\\w{3}\\b"; // Compile rules into pattern objects Pattern p = (regex); // Get matcher object through pattern object Matcher m = (s); while (()) { (()); } // Note: You must find() first before group() // IllegalStateException: No match found // String ss = (); // (ss); } }
PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools./regex/javascript
Regular expression online generation tool:
http://tools./regex/create_reg
For more information about Java algorithms, please check out the topic of this site:Complete collection of Java regular expression techniques》、《Summary of Java characters and string operation techniques》、《Java Data Structure and Algorithm Tutorial》、《Summary of Java operating DOM node skills"and"Summary of Java files and directory operation skills》
I hope this article will be helpful to everyone's Java programming.