Seven
, writing regular Expressions
Tips
:
Regular
write a three-step walk:
1
)
check what?
2)
check how much?
3)
where to find out?
1. Regular
An expression consists of:
Regular expressions are text patterns that consist of ordinary characters, such as characters A through z, and special characters (called metacharacters). A regular expression, as a template, matches a character pattern to the string you are searching for. 2 , check how much? (Meta -character-qualifier )
Qualifiers can specify how many times a given component of a regular expression must appear to satisfy a match.
* Match previous sub-expression 0 or more times (0 to more)
+ Match Previous sub-expression one or more times (1 to many)
? Match the preceding subexpression 0 or one time (0 or 1)
{n} matches determined n times
{N,} matches at least n times {4,} minimum match 4 times
{n,m} matches at least n times and matches M-Times {3,7}
Example code:
Example 1: Matching a continuous 11-digit number
Example 2: Match character ABC occurs in a string of 0 or 1 times
Example 3: Query numeric characters appear at least 4 times in a string
Example 4: Query text character (A-Z) string occurrences in 3, 7
3. Matching principle: 1) greedy match
In regular expressions, we call this greedy match when we match as many as possible. (Default principle) 2) non- greedy match
In regular expressions, you can pass the? Question mark to change our greedy matching principle, we call this principle non-greedy matching principle
Example code: plus? After that, will be separated by 3 times, less than 3 times, will omit
Do not add? , matches a number of 3-9 characters and separates it by a group of 9
4. Check what (meta-character--match )
Character matches are used to match one or some characters
What we call a character cluster in a regular expression that is enclosed in parentheses
l [A-z]: matches any character in a-Z
l [A-z]: matches any character in a-Z
l [0-9]: Match any number in the 0-9 \d
l [0-9a-z]: matches any character in 0-9 or A-Z
l [0-9a-za-z]: matches any character in 0-9 or A-Z or a-Z
l [ABCD]: match character A or character B or character C or character D
l [1234]: match the number 1 or the number 2 or the number 3 or the number 4
In the regular expression of the character cluster, you can use the upper key ^ ^ to take the inverse
l [^a-z]: match any character except A-Z
l [^0-9]: matches any character except 0-9 \d
l [^ABCD]: match any character except the letter Abcd
L \d: matches a numeric character [0-9]
L \d: Matches a non-numeric character [^0-9]
L \w: matches any word character that includes an underscore [0-9a-za-z_]
L \w: matches any non-word character [^\w]
L \s: matches any white space character spaces, tab stop, etc.
L \s: matches any non-whitespace character [^\s]
L. : matches any single character except "\ n"
Sample code
Example 1: Match any numeric character
Example 2: Match any of the characters in a ASW three character
Example 3: Match all the spaces
Example 4: Matching three consecutive uppercase letters
Example 5: Matching Chinese characters
5. from where to check ( Meta character --Locator)
A locator can pin a regular expression to the beginning or end of a line. You can also create a regular expression that appears only within a word or only at the beginning or end of a word.
Example 1: Matching 11-digit phone number
Example 2: Matching a fixed phone
020-87654321 38 Structure
0451-7654321 47 Structure
^\d{3}-\d{8}$
^\d{4}-\d{7}$
^\d{4}-\d{8}$
^: Top Gear (start position)
$: End Position
\b: Represents the word boundary position
\b: Represents a non-word boundary
Example 3: Word Boundaries and non-word boundaries
6. Special Characters
1) escape character
In regular expressions, \ is used to match certain special characters
/ + * ? ( ) [] {} . ^ $ \
Example code:
Example 1: match [left symbol]
Example 2: Match the. number in the URL
Example 3: Matching by character cluster. No.
7 , Selection Character
In regular expressions, you can represent selectors by |
Example 1: Matching Hellojavascript or hellophp
8. three special uses
(? =): Forward pre-check
(?!) : Negative pre-check (reverse pre-check)
(?:): output content without capturing 1) forward pre-check
2) negative to pre-check
3) output but not capture
Eight , regular expression--stutter program
Stutter Program
I... Yes, I am. A ... Handsome handsome handsome ... Brother! ";
Turn
I am a handsome guy!
Example code:
Description
Solution:
1) first remove all the. Numbers from the string to replace with the null character
2) match all repeated characters to the original content by sub-expression and put in a buffer
3) In the Replace method, the second argument, you can use $n (a positive integer of n>=1&&n<=99) to refer to the contents of the N-number buffer. Nine , writing Regular Toolbox
Code please refer to tools.html 10 , Job summary
Zuoye01.html
Zuoye02.html
...
Zuoye04.html
snake.html 11 , Operation
Mailbox Verification
rxy @itcast . CN
IP authentication
192.168.114.10
0-223 First place
0-255 after three digits
20150209--js consolidation and strengthening 6-02