First, a preliminary understanding the regular expression is a logical formula for string manipulation, which is to make a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of these specific characters. A regular expression is a text pattern that describes one or more strings to match when searching for text from the online collection of two examples, it feels good to paste an example This\s+is\s+text---> This is text
Note the \s+ in the string.
Matches the word "This" after the \s+ can match more than one space,
After matching is string,
After that, \s+ matches multiple spaces and then follows the text string. Example two ^\d+ (\.\d+)? ----> 2.21
^ defined with what to start with
\d+ match one or more numbers
? Setting the options in parentheses is optional
\. Match "."
Instances that can be matched: "5", "1.5", and "2.21". So a string is actually a simple regular expression. Second, basic grammar
. Any string
\ "\\\\" matches "\", "\ \" ("Match" (".
^ match the beginning of the input string
$ match the position at the end of the input string
*  &NB SP; 0 or more times matches the preceding character or subexpression
+ matches one or more occurrences of the preceding character or sub-expression
? 0 or one time matches the preceding character or subexpression
{n} n is a non-negative integer. Exactly matches n times
{n,m} matches at least N times, up to M times.
{n,} match at least n times
? non-greedy "pattern match search to the shortest possible string,
The default "greedy" pattern matches the search for the longest possible string
in the string "Oooo", "o+?" Only a single "O" is matched, and "o+" matches All "O".
X|y match x or Y. For example, ' Z|food ' matches ' z ' or ' food '. ' (z|f) Ood ' matches "Zood" or "food".
[XYZ] matches any one of the characters contained. For example, "[ABC]" matches "a" in "plain".
[^xyz] inverse character Set. Matches any characters that are not contained. For example, "[^abc]" matches "plain" in "P", "L", "I", "N".
A [A-z] character range. Matches any character within the specified range. For example, "[A-z]" matches any lowercase letter in the range "a" to "Z".
[^a-z] Reverse range character. Matches any character that is not in the specified range. For example, "[^a-z]" matches any character that is not in the range "a" to "Z".
\b Matches a word boundary, which is the position between the word and the space. For example, "er\b" matches "er" in "never", but does not match "er" in "verb".
\b A non-word boundary match. "er\b" matches "er" in "verb", but does not match "er" in "Never".
\d numeric character matching. equivalent to [0-9].
\d non-numeric character matching. equivalent to [^0-9].
Third, examples
1 /**2 * Regular Expression Test3 * @authorWangchong4 * @date September 29, 2017 morning 11:25:275 */6 Public classregexmatches {7 8 Public Static voidMain (String args[]) {9 TenSYSTEM.OUT.PRINTLN ("Test boundary match $ and ^"); One ASYSTEM.OUT.PRINTLN ("Test \ \"); -System.out.println ("abc\\". Matches (". *\\\\$")); -System.out.println ("Test ^"); theSystem.out.println ("abc\\". Matches ("^a.*"))); - -System.out.println ("instance one: regular expression filters the correct mobile phone number in a piece of text"); - System.out.println (); + -String Messge = "Dear Mobile User 18352537674 Hello, your account 132419255 remaining balance 111111"; +SYSTEM.OUT.PRINTLN ("Message content:" +messge); APattern regex1 = Pattern.compile ("\\d+ (1\\d{10}) \\d+"); atMatcher Matcher =Regex1.matcher (messge); - - while(Matcher.find ()) { -System.out.print ("Extracted phone Number:" + matcher.group (1)); - } - in } - to}
Run results
Rookie growth Diary: Java Basics 4---regular expressions