Regular Expressions
Role: verify whether a string matches a rule
Code:
// 1. regex validation rules String reg = "regex" ; // the Pattern, Matcher in java.util package @ 2 compiled regular expression rules the Pattern = pattern of Pattern.compile (REG); / / 3. rules and verification string comparison Matcher m = pattern.matcher ( "comparison character string" ); // 4. verify // .matches () / fully matched, .fing () / non-full match (comprising the success) IF (m.matches ()) { // to true successful match } the else { // to false match is unsuccessful }
Basis style regular expressions
formula | significance | formula | significance |
---|---|---|---|
{n} | Before a letter appears n times | {n,} | Before a letter appears n times n or more |
{n,m} | A letter that appears before the time n ~ m | {n} | Parentheses class content appears n times |
[a-z] | Lower case letters | [A-zA-Z] | Uppercase and lowercase letters |
[0-9] | digital | [_] | Underline |
\w | Corresponds to [a-zA-Z0-9] | \W | In addition to \ capacity class than w |
\d | Equivalent to [0-9] | \D | In addition to all classes of digital content |
\s | Blank | | | or |
* | Equivalent to {0} | + | Equivalent to {1} |
? | Equivalent to {0,1} | ^ | What began with |
$ | To what end |
pay attention:
-
[0-20] / judgment is 0;
-
[ AZ] [Az] l, 5} {/ a lowercase, uppercase 5 times;
-
For example, when writing Java \ w need to write to \ \ w, because in Java \ has a special significance;
-
. "" In java written as " \ \ .";
-
^ ............ $ / in some regular expression ^ $ emergence will appear, which means that with the same start, the same end again. Class content is secured between the two
The writing style and .matches () effect .find () statement in the same. Meanwhile, the regular expression can also be used in other languages.