Python Basic Learning Log day5--re module

Source: Internet
Author: User

Common Regular Expression symbols

'.'default match any character except \ n, if flag Dotall is specified, matches any character, including line break'^'Match the beginning of the character, if you specify the flags MULTILINE, this can also match on (R"^a","\nabc\neee", flags=Re. MULTILINE)'$'Matches the end of a character, or E.search ("foo$","BFOO\NSDFSF", flags=Re. MULTILINE). Group () can also'*'Matches the character preceding the * number 0 or more times, Re.findall ("ab*","Cabb3abcbbac") results are ['ABB','AB','a']'+'Matches the previous character 1 or more times, Re.findall ("ab+","Ab+cd+abb+bba"Results'AB','ABB']'?'match a previous character 1 or 0 times'{m}'matches the previous character m times'{n,m}'Matches the previous character N to M times, Re.findall ("ab{1,3}","ABB ABC abbcbbb") Results'ABB','AB','ABB']'|'Match | left or | Right character, re.search ("abc| ABC","ABCBABCCD"). Group () results'ABC''(...)'Group matching, Re.search ("(ABC) {2}A (123|456) C","abcabca456c"). Group () result abcabca456c'\a'Match only from the beginning of the character, Re.search ("\AABC","ALEXABC") is not matched to the'\z'match character end, same as $'\d'Match number 0-9'\d'match non-numeric'\w'Match [a-za-z0-9]'\w'Match non-[a-za-z0-9]'s'Match whitespace characters, \ t, \ n, \ r, Re.search ("\s+","ab\tc1\n3"). Group () results'\ t' '(? P<name>, ...)'Group Matching Re.search ("(? P<province>[0-9]{4}) (? P<city>[0-9]{2}) (? P<BIRTHDAY>[0-9]{4})","371481199306143242"). Groupdict (" City"Results'Province':'3714',' City':'Bayi','Birthday':'1993'}

The most commonly used match syntax

Re.match match Re.search match contains re.findall put all matching characters into the list of elements returned re.splitall to match the characters as list separators re.sub      match characters and replace

The haunting of the backslash
As with most programming languages, "\" is used as an escape character in regular expressions, which can cause a backslash to be plagued. If you need to match the character "\" in the text, then 4 backslashes "\\\\" will be required in the regular expression expressed in the programming language: the first two and the last two are used to escape the backslash in the programming language, converted to two backslashes, and then escaped in the regular expression into a backslash. The native string in Python solves this problem well, and the regular expression in this example can be expressed using R "\ \". Similarly, a "\\d" that matches a number can be written as r "\d". With the native string, you no longer have to worry about missing the backslash, and the expression is more intuitive.

Re. I (re. IGNORECASE): Ignore case (full notation within parentheses, same as) M (MULTILINE): Multiline mode, change '^' and '$ ' ' behavior (see) S (dotall): Point any matching pattern, change '. ' The Act

Python Basic Learning log day5--re module

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.