Half an hour proficient in regular expression Classic example introduction _ Regular Expression

Source: Internet
Author: User
Tags control characters lowercase
At the beginning, you have to say ^ and $ they are used to match the start and end of a string, respectively, as illustrated below
If you're a novice, guess you're canceling the regular expression 30 minute introductory tutorial
"^the": The beginning must have "the" string;
"Of despair$": The end must have a string of "of despair";
So
"^abc$": a string that requires the beginning of ABC and the end of ABC, which is actually only an ABC match
"Notice": matches a string containing notice
You can see if you don't use the two characters we mentioned (the last example), which means that the pattern (regular expression) can appear anywhere in the string being tested, you don't lock him on either side
Then, say ' * ', ' + ', and '? ',
They are used to indicate the number or order in which a character can occur. They said separately:
"Zero or more" equals {0,},
"One or more" equals {1,},
"Zero or one." Equivalent to {0,1}, here are some examples:
"ab*": and Ab{0,} synonymous, matching with a start, followed by 0 or n B of the string ("a", "AB", "abbb", etc.);
"ab+": and Ab{1,} synonymous with the same as above, but at least one B exists ("AB", "abbb", etc.);
"AB": Synonymous with ab{0,1}, can be without or only a B;
"a?b+$": a string that matches the end of one or 0 a plus more than one B.
Points, ' * ', ' + ', and '? ' Just the character in front of it.
You can also limit the number of characters appearing in curly braces, such as
"Ab{2}": Require a must be followed by two B (one also can not be less) ("ABB");
"Ab{2,}": Require a must have two or more than two B (such as "ABB", "abbbb", etc.);
' ab{3,5} ': Requires that a 2-5 B ("abbb", "abbbb", or "abbbbb") be followed by a.

Now we put a few characters into parentheses, such as:
"A (BC) *": Match a followed by 0 or a "BC";
"A (BC) {1,5}": one to 5 "BC."
There is also a character ' │ ', which is equivalent to an OR operation:
"Hi│hello": matches a string containing "hi" or "hello";
"(B│CD) EF": Matches a string containing "bef" or "cdef";
"(a│b) *c": matching contains such multiple (including 0) A or B, followed by a C
The string;
A point ('. ') can represent all single characters, excluding "\ n"
What if you want to match all the individual characters, including "\ n"?
Yes, with ' [\ n.] ' This pattern.
"A.[0-9]": A plus one character plus a number 0 to 9
"^. {3}$: Three any character end.

The content enclosed in brackets matches only one single character
"[AB]": Match a single A or B (as with "a│b");
[a-d]: a single character that matches ' a ' to ' d ' (same as ' a│b│c│d ' and ' [ABCD] ' effect); Generally we use [a-za-z] to specify characters for a single case of English
' ^[a-za-z] ': matches a string that starts with the uppercase and lowercase letters
' [0-9]% ': matches a string containing x-percent
", [a-za-z0-9]$": matches a string that ends with a comma plus a number or letter
You can also put the characters you don't want in the brackets, you just need to use ' ^ ' inside the parentheses.

-------------------------------------------------------------------------
^ and $ are used to match the start and end of a string, respectively
-------------------------------------------------------------------------

Example 1 ^<b> must be preceded by a "<b>" string;
Example 2 </b>$ end must have "</b>" string;
Example 3 ^abc$ a string with ABC beginning and ending with ABC, which is actually only an ABC match
Example 4 ABC without symbols matches a string containing ABC


-------------------------------------------------------------------------
* + and? Used to indicate the number or order in which a character can occur. They said separately
-------------------------------------------------------------------------

{0,} = * Example 1 ab{0,} matches O-n ("A", "AB", "ABB", "abbbbbbbbbbbbbbbbb", infinite ...) after the beginning of a. )
{1,} = + Example 2 Ab{1,} matches with 1-n ("AB", "ABB", "abbbbbbbbbbbbbbbbb", infinite ...) after the beginning of a. )
{0,1}=? The example 3 ab{0,1} matches the occurrence of O-1 ("a", "AB") after the beginning of a.
Example 4 a{0,1}b+$ matches a string with 0 or 1 a plus a B end. ("B", "AB")

Note (2 kinds of writing)
Ab{0,} can also be written as ab*
Ab{1,} can also be written as ab+
ab{0,1} can also be written as AB?
A{0,1}b+$ can also be written as a?b+$


(1) 1 points, ' * ' + ', and '? ' Just control the number of occurrences of the character in front of it.
2 {n,n} several to several times {0} o times
3{} This cannot be a negative number

(2) The number of times can be modified

Example 5 ab{2} requires that a must be followed by a two B ("ABB");
Example 6 ab{2, require a must have two or more than two B ("ABB", "abbbb", etc.);
Example 7 ab{3,5} requires that a 2-5 B ("abbb", "abbbb", or "abbbbb") be followed by a.

(3) followed by multiple characters used ()
Example 8 A (BC) * matches a followed by a 0 or a "BC"; Of course you can also write "a (BC) {0,}"
Example 9 A (BC) {1,5} matches 1 to 5 "BC."

-------------------------------------------------------------------------
│ is equivalent to or used to represent 1 or more or
-------------------------------------------------------------------------

Example 1 a│b matches a string containing "A" or "B";
Example 2 (a│b) C matches a string containing "AC" or "BC";
Example 3 (a│b) *c match contains (including 0-1) A or B, followed by a C

-------------------------------------------------------------------------
. Can represent all the single characters
-------------------------------------------------------------------------
. Excluding "\ n" spaces with a space and a character [\ n.] Multiple spaces +1 characters [\n\n\n\n\n\n.]

Example 1 A.[0-9] a plus one character plus a number of 0 to 9
Example 2 ^. {3}$ three end of any character

-------------------------------------------------------------------------
The bracketed contents of ' [AB] ' match only one single character
-------------------------------------------------------------------------

Example 1 [ab] matches a single A or B (same as "a│b");
Example 2 [a-d] matches a single character of ' A ' to ' d ' (same as "a│b│c│d" and "[ABCD]" effect); Generally we use [a-za-z] to specify characters for a single case of English
Example 3 ^[a-za-z] matches a string that starts with a case letter
Example 4 [0-9]% matches a string containing a percent of X
Example 5, [a-za-z0-9]$ matches a string that ends with a comma plus a number or letter
Example 6%[^a-za-z]% matches a string containing a (non) letter in the two percent sign.

You can also put the characters you don't want in the brackets, you just need to use ' ^ ' inside the parentheses.

Important 1:^[content] ^ begins outside [], indicating the beginning of the content
Point 2:[^ content] ^ opening in [] means excluding the contents (^ non-meaning)
Point 3: Matches the string containing these characters. In the brackets [*\+?{}.] or the ' symbol matches the failure parenthesis only to a single character
Point 4: [] It's best to use it as the first character in the list (probably following ' ^ ')
Point 5: [] contains the '-' which is best to place it at the front or end, or the second end of a range [a-d-0-9] in the middle of '-' will be valid.



-------------------------------------------------------------------------
\b and \b 1 match a word right boundary 2 match non word boundary
-------------------------------------------------------------------------

Example 1 ' ve\b ': Can match Love's ve but not match very have ve
Example 2 ' ov\b ': can match the OV in love and not match Ovry ov

-------------------------------------------------------------------------
\d and \d
-------------------------------------------------------------------------

Example 1 \d matches a numeric character. equivalent to [0-9].
Example 2 \d matches a non-numeric character. equivalent to [^0-9].

-------------------------------------------------------------------------
\w and \w
-------------------------------------------------------------------------
Example 1 \w matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '
Example 2 \w matches any non-word character that includes an underscore. Equivalent to ' [^a-za-z0-9_] '.

-------------------------------------------------------------------------
Match nonprinting characters
-------------------------------------------------------------------------
Character meaning
\CX matches the control characters indicated by X. For example, \cm matches a control-m or carriage return character. The value of x must be one-a-Z or a-Z. Otherwise, c is treated as a literal ' C ' character.
\f matches a page feed character. Equivalent to \x0c and \CL.
\ n matches a newline character. Equivalent to \x0a and \CJ.
\ r matches a carriage return character. Equivalent to \x0d and \cm.
\s matches any white space character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].
\s matches any non-white-space character. equivalent to [^ \f\n\r\t\v].
\ t matches a tab character. Equivalent to \x09 and \ci.
\v matches a vertical tab. Equivalent to \x0b and \ck.

-------------------------------------------------------------------------
Example
-------------------------------------------------------------------------

A regular expression that matches the end-end whitespace character: ^s*|s*$
Regular expression matching an email address: w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *
Regular expressions that match URL URLs: [a-za-z]+://[^s]*

Match account number is legal (beginning of letter, allow 5-16 bytes, allow alphanumeric underline): ^[a-za-z][a-za-z0-9_]{4,15}$
Match domestic phone number: d{3}-d{8}|d{4}-d{7} match form such as 0511-4405222 or 021-87888822 920-209 642-964
Matching Tencent QQ Number: [1-9][0-9]{4,} 1+ after four for the number started, that is, 10000
Match China ZIP Code: [1-9]d{5} (?! d) China postal code is 6 digits
Matching ID: d{15}|d{18} commentary: China's ID card is 15-or 18-digit
Matching IP address: d+.d+.d+.d+ Commentary: Useful when extracting IP addresses





-------------------------------------------
^ $//start to end
+//continuous 1-n (ie-{1,})
-? Represents a negative and non-negative (i.e.-{0,1})
[0-9]*//Represents the front digits 0-n (that is, [0-9]{0,})
.? Indicate a little or no point
[^//non-contents inside

[A-z]//Match all lowercase letters
[A-z]//Match all uppercase letters
[A-za-z]//Match all the letters
[0-9]//Match all numbers 0-9 integers
[0-9.-]//Match all numbers, periods and minus signs

------------------------------------------------


^[a-za-z0-9_]+$//All containing more than one letter, number or underscore string//example together Aa0_a001a_
^[0-9]+$//All positive numbers (also can be said to be non-negative integers)//Give example 345500687008099900999
^-? [0-9]+$//all integers (including negative integers and integers)//example-43443 or 43443
^-? [0-9]*.? [0-9]*$//all decimals (including the number of digits before and after decimal decimal points are infinitely long)//example-10.00 or 100000.0000
If there is no number after the decimal point, so the front plus a.? To determine whether there are small points, according to the truth can not be required. It's redundant.
Because this is a special judge of decimal, if there is no decimal point, but also called to pay the number?
[^a-z]//All characters except lowercase letters
[^/^]//All characters except the "/" and "^" characters
[^ "']//all characters except double quotes (") and single quotes (')

This article is based on examples, to facilitate experienced friends, if there is no relevant experience to see our regular expression 30-minute introductory tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.