Java Regular Expressions
Java Regular Expression
java.util.regex.Patternjava.util.regex.Matcher
1. Match
Match indicates that the entire string needs to be matched from the beginning to the end of the string.
String content = "Welcome, bob! "; Content. match ("bob"); // falsecontent. match (". * bob ") // falsecontent. match (". * bob. * ") // trueString str =" test@yahoo.com.cn "; Pattern pattern = Pattern. compile ("[\ w \\. \-] + @ ([\ w \-] + \\.) + [\ w \-] + ", Pattern. CASE_INSENSITIVE); Matcher matcher = pattern. matcher (str); boolean a = matcher. matches (); // returns true when matching
2. Find
Boolean B = matcher. find (); // The string containing the regular match is true // find all matched strings while (matcher. find () {String extracted = matcher. group (0 )}
3. Replace
matcher.replaceFirst("")matcher.replaceAll("");
4. Group
Group (0) represents the entire expression String line = "# horoscope #20171013"; String pattern = "\\# (\ p {L }*) \\# (\ d +) "; // \ p {L} matches unicode any kind of letter from any language // creates the Pattern object Pattern r = Pattern. compile (pattern); // now create the matcher object Matcher m = r. matcher (line); if (m. find () {System. out. println ("Found value:" + m. group (0); // "# constellation fortune #20171013" System. out. println ("Found value:" + m. group (1); // horoscope System. out. println ("Found value:" + m. group (2); // 20171013} else {System. out. println ("no match ");}