Python Development Module Basics: Regular Expression &re module

Source: Internet
Author: User

One, regular expression

1. Character group: [0-9][a-z][a-z]

The various characters that may appear in the same location make up a group of characters, expressed in a regular expression using []
Characters are divided into classes, such as numbers, letters, punctuation, and so on.
If you now ask for a position "only one number can appear", then the character in this position can only be one of the 10 numbers, 0, 1, 2...9.
Can be written as a match for this [0-5a-ea-z] fetch range

2. Characters

1 #!/usr/bin/python Env2 #_*_coding:utf-8_*_3 4 . Match any character other than line break5 \w match letters or numbers or underscores6 \s matches any whitespace character7 \d Matching numbers8 \ n matches a line break9 \ t matches a tabTen \b Matches the end of a word One^match the start of a string A $ matches the end of a string - \w matches non-alphabetic or numeric or underlined - \d matches non-numeric the \s matching non-whitespace characters -a|b match character A or character B - () matches the expression in parentheses, and also represents a group - [...] Match characters in a character group +[^...] Match all characters except characters in a character group
3. quantifiers
1 # !/usr/bin/python Env 2 # _*_coding:utf-8_*_ 3 4 quantifier  Usage Description 5 *    repeat 0 or more times 6 +    Repeat once or more 7 ?   Repeat 0 times or once 8{n} repeats n times 9{n,}    repeats n or more times {n,m}    Repeat N to M times

4.. ^$

 1  #  !/ Usr/bin/python env  2  #  _*_coding:utf-8_*_  5   regular       The matching character match result description  6  Sea. Haiyan Hai Jiao Haidonghai Yan Haijiao haidong matches all  "  Sea.   "       7  ^ Sea. Haiyan Hai Jiao Haidong Haiyan only matches from the beginning  "  Sea.   " 8  Sea. $ petrel Hai Jiao Haidonghaidong only matches the end of  " 
    
      Sea. $ 
      " 

5.*+? {}

1 #!/usr/bin/python Env2 #_*_coding:utf-8_*_3 regular matching character matching result description4Li.? Li Jie and Buddy and Lee two sticks Li Jie/Li Lian/Lee? means repeat 0 or one time, that is, match only"Li"an arbitrary character later5Lee. * Li Jie and buddy and Li two sticks Li Jie and buddy and Li two sticks * means repeat 0 or more times, that is, match"Li"after 0 or more arbitrary characters6Li + li Jie and buddy and Li two sticks Li Jie and buddy and Li two sticks + means repeat once or multiple times, i.e. only match"Li"1 or more of the following characters7Lee {Li Jie} and Buddy and Li two sticks Li Jie and/buddy/Lee Sticks {matches any character 1 to 2 times8 9Note: the front *,+,, etc. are greedy matches, that is, match as much as possible, then add the number to make it an inert matchTen regular matching character matching result description OneLee. *? Li Jie and Buddy and Lee two stick Lee/Lee/Li inert match

6. Character Set [][^]

1 #!/usr/bin/python Env2 #_*_coding:utf-8_*_3 4 regular matching character matching result description5Lee [Jackie Ying two stick]* Li Jie and Buddy and Lee two sticks Li Jie/buddy/Lee stick to show match"Li"word back [Jackie two stick] character any time6Lee [^ and]* Li Jie and Buddy and Lee two sticks Li Jie/buddy/Lee stick means match one not"and the"the characters any time7[\d] 456bdha3 4/5/6/3Matches any number to match 4 results8[\d]+ 456bdha3 456/3 matches any number and matches to 2 results

7. Grouping () or | and [^]

1 #!/usr/bin/python Env2 #_*_coding:utf-8_*_5 The ID number is a 15 or 18 character string, and if it is 15 bits all??? Number, the first can not be 0, if 18 bits, the first 17 digits are all numbers, the last may be a number or x, below we try to use the regular to represent:6 regular matching character matching result description7^[1-9]\d{13,16}[0-9x]$ 110101198001017032 110101198001017032indicates that a correct ID number can be matched8^[1-9]\d{13,16}[0-9x]$ 1101011980010170 1101011980010170indicates that it can also match this number, but this is not a correct ID number, it is a 16-digit number9^[1-9]\D{14} (\d{2}[0-9x])? $1101011980010170 false now does not match the wrong ID number () indicates the grouping, will \d{2}[0-9x] cent Into a group, you can constrain them as a whole. 0-The number of occurrences1 playsTen^ ([1-9]\d{16}[0-9x]| [1-9]\d{14}) $110105199812067023 110105199812067023 indicates first match [1-9]\d{16}[0-9x] if there is no match on the match [1-9]\d{14}

8. Escape character \

1 #!/usr/bin/python Env2 #_*_coding:utf-8_*_3 4In regular expressions, there are many special meanings of metacharacters, such as \d and \s, if you want to match the normal"\d"Instead of"Digital"You need to"\ "To escape and become ' \ \ '. 5In Python, the regular expression, or the content to be matched, is in the form of a string, which has a special meaning in the string and that itself needs to be escaped. So if it matches once,"\d", in the string to be written'\\d', then the regular is going to be written"\\\\d", so it's too much trouble.
This time we're going to use R.'\d'This concept, at this time the regular is R'\\d'you can do it. 6 regular matching character matching result description7 d \d False because \ is a character with special meaning in the regular expression, so to match the \d itself, the expression \d cannot match 8 \\d \d True escape \ Then change to \ \ to match9 "\\\\d" '\\d'True If in Python, the string'\ ' also needs to be escaped, so every string'\'and you need to escape once .TenR'\\d'R'\d'True to precede the string with R, so that the entire string is not escaped

9, greedy match

1 #!/usr/bin/python Env2 #_*_coding:utf-8_*_3 4 greedy match: matches the string as long as possible when matching matches, by default, greedy match5 regular matching character matching result description6<.*> <script>...<script> <script>...<script>The default is greedy match mode, which matches as long as possible string7<.*?> R'\d'<script>/<script>Plus? To convert the greedy match pattern to a non-greedy match pattern, match the shortest possible string8 9 several commonly used non-greedy matching patternTen*repeat any number of times, but repeat as little as possible. One+repeat 1 or more times, but repeat as little as possible A ?? repeat 0 or 1 times, but repeat as little as possible - {n,m}? Repeat N to M times, but as few repetitions as possible - {n,}? Repeat more than n times, but repeat as little as possible the  -.*? usage - . is any character -*is to take 0 to infinite length + ? non-greedy mode.  - Where together is to take as little as possible any character, generally not so alone, he mostly used in: +.*? x A  atis to take any length of the preceding character until an X appears

Python Development Module Basics: Regular Expression &re module

Related Article

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.