Getting Started with regular expressions
Str.matches ("\\d+"); Returns true from 1 to multiple digits
What is a regular expression? is a string, a string consisting of a meta-character "pre-defined character" and a normal character and quantifier.
What is the function of regular expressions? Used to implement matching or filtering of strings.
The meta-characters of the regular expression are as follows:
\d Number (digit)
\d Non-digital
\s Spaces (space)
\s Non-whitespace
\w single character: a symbol in an alphanumeric underline (word)
\w non-single-character: a symbol outside of single-character
. An arbitrary character [except \ n \ r]
^regex$ ^ means start $ means end
| Or
The quantifiers are as follows:
+ 1 or more characters
* 0 or more characters
? 0 or 1 characters
{n} exactly n characters
{N,} at least n characters
{n,m} at least N, up to M characters
Character:
Use [] to list the characters that appear
[Ac?_6] represents one of 5 characters
[0-9] represents one of the 0-9,-Indicates the range
So \w is equivalent to [a-za-z0-9_]
Regular applications:
Common methods for the regular correlation of string classes
Boolean matches (regex); match : match strings according to regular requirements
String[] Split (regex); split : Splits a string according to regular requirements
Split Example:
String str= "I love! You? ";
String regex= "[\\s!? \\d]{1,} "; One or more spaces and exclamation marks, question mark, number
String[] S=str.split (regex);
for (String tem:s) {
println (TEM);
}
println ("number" +s.length);
Accumulation:
Non-negative integer [0-9]+
positive integers [1-9]\\d* OR [1-9][0-9]*
After the beginning of the letter, follow 3-6 arbitrary characters [a-za-z]. {3,6} equivalent [A-za-z]{1}. {3,6}
Replace
String newstr = Str.replaceall (string regex,string replament); Replace Regex with Replament
Accumulation:
Or, either the NBA or the CBA:NBA|CBA.
Phone number: [1][3578][0-9]{9}
Email: [Email protected]+[.]. + is wrong [a-za-z0-9]\\w*[@][a-za-z0-9]+[.] [A-za-z] {2,3}
Example:
Java.util.Pattern
Pattern.matches (Regex,email); Equivalent to Email.matches (regex);
Filter
Not to be continued
Java Basics-Regular expressions