1. Regular expressions
A regular expression is a sequence of formatting rules that describes a string using a series of predefined special characters, and then uses that formatting rule to match a string for formatting requirements.
1) "." and "\"
"." Any one character in the regular expression.
"\" is an ideographic character in a regular expression, and when we need to describe a special character that has been used by a regular expression, we can convert it to its original meaning by using "\".
"\" also has some pre-defined special content in regular expressions:
\d: Represents any number
\w: denotes any single word character (only numbers, letters, underscores)
\s: Represents any white space character (\ t \ r \ \f \x0b)
\d: Represents any non-numeric character
\w: Represents any non-word character
\s: Represents any non-whitespace character
2) "Character Set []"
"[]" is used to describe a single character, inside the square brackets can define the contents of this character, can also describe a range. For example:
[ABC]: Indicates that the character can only be A or B or C
[123]: Indicates that the character can only be 1 or 2 or 3
when we need to describe all lowercase letters, we can use the range [A-z], which means that the character can be any lowercase letter.
You can also use [0-9] to indicate that the character can be any number.
You can also select within multiple scopes. For example, [a-za-z0-9_] means that the character can be any letter, a number, and an "underscore."
3) "*", "+", "?" "
usually we need to describe the string will have a lot of repeating elements, but do not need to strictly limit the number of occurrences, we can use "*", "+" these quantifiers.
For example, an e-mail address, a number of characters are allowed to appear before the "@" character as the user name. In this case we can use "\w+" to describe at least one word character here.
"+": indicates that content can appear at least 1 consecutive times
"*": Indicates content appears 0-several times
"?" : Indicates that the content appears 0-1 times
4) {n}, {n,}{n,m}
In addition to the quantifiers mentioned earlier, we sometimes need to ask for specific requirements for the number of times the content appears. such as mobile phone number. The number we ask for is not a vague concept, but it must require 11 bits. Another example is that when we ask the user to enter a password, the password is 6-15 bits. When you encounter such problems, we can use:
{n}: Indicates that content must appear n times
{n,m}: Indicates content appears n-m times
{N,}: Indicates content appears at least n times
For example, \d{11} indicates that a number can only appear in 11 digits, which solves the problem above.
5) Grouping
When describing a phone number, there is an area code in front of it, and the area code can be 0086 or +86, so how do we choose between the two strings? We can then use the Grouping "()".
() can be viewed as a whole, () can use the ' | ' to represent or relate. For example, (+86|0086) means that this can be +86 or 0086.
6) "^" and "$"
Represents a whole by adding "^" at the beginning of the regular expression and adding "$" to the end. If they are not used, the regular expression matches only parts of a string that conform to the formatting rules, but using them requires that the string must satisfy the formatting rule from beginning to end.
For example, ^\w{8,10}$ indicates that a whole string can only have 8-10 word characters.
7) Use regular expressions to be aware of:
① the regular expression of the mailbox format @ No special meaning, can write directly, also can [@]
When ② uses a Java string to describe a regular expression, a conflict arises, that is, how to correctly describe the regular expression. ”。
causes:
in the regular expression we want to describe a ". "But". "There is a special meaning in the regular expression and he represents any character, so we want to describe it in the regular expression." "The righteous will be written." "But when we use a Java string to describe a regular expression, because". "There is no special meaning in the Java string, so Java thinks we are writing string s=" \. "There is a grammatical error, because". "There is no need to escape, which creates a conflict.
Processing: Our practical purpose is simply to let Java's string description "\. "And because in Java" \ "has a special meaning, to represent the escape character we only need to escape" \ "as a simple slash, you can describe" \. " "So we use Java to describe" \. "The correct notation is string s=" \ \. ";
③ Jo Zheng The expression does not write ^ or $, the regular expression represents the matching part of the content, plus the representation of the right match.
Case 4:
650) this.width=650; "style=" width:672px;height:411px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M02/6d/e6/wkiol1vuxu_ioeypaannqlbjlvw820.jpg "width=" 710 "height=" 421 "alt=" wkiol1vuxu_ioeypaannqlbjlvw820.jpg "/ >
This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1657973
Java Core API--3 (regular expression)