[Python learning] Regular expressions

Source: Internet
Author: User

RE module functions
Re-module functions and methods of regular expression objects
Match (pattern,string,flags=0) attempts to match a string with a pattern of regular expressions with optional tokens. If the match succeeds, the matching object is returned, and if it fails, it returns none.
Search (pattern,string,flags=0) searches for the first occurrence of a regular expression pattern in a string using an optional tag. If the match succeeds, the matching object is returned, or none if it fails
FindAll (pattern,string [, flags]) ① finds all (non-repeating) occurrences of the regular expression pattern in a string and returns a matching list
Finditer (pattern,string [, flags]) ② is the same as the FindAll () function, but returns an iterator instead of a list. For each match, the iterator returns a matching object
Split (pattern,string,max=0) ③ the Split function splits the string into a list based on the pattern delimiter of the regular expression, and then returns the list of successful matches, separating the maximum operations Max times (the default is to split all successful locations)

Re module functions and regular Expression object methods
Sub (pattern,repl,string,count=0) ③ replaces all occurrences of the pattern of the regular expression with REPL in the string, unless count is defined, otherwise the occurrence is replaced (see also SUBN () function, which returns the number of substitution operations)

Common methods of matching objects (view documents for more information)
Group (num=0) returns the entire matching object, or a specific subgroup numbered NUM
Groups (Default=none) returns a tuple containing all matched subgroups (returns an empty tuple if there is no successful match)

#-*-coding:utf-8-*-ImportRe#match ()M0 = Re.match ('AA','AA is not AA')PrintM0.group ()#Search ()M2 = Re.search ('AA','AA is not AA')PrintM2.group ()#FindAll ()M4 = Re.findall ('AA','Aa1 Aa2 Aa3')PrintM4#Sub ()M5 = Re.sub ('AA','BB','Aa1 Aa2 Aa3')PrintM5#subn ()M5 = Re.subn ('AA','BB','Aa1 Aa2 Aa3')PrintM5

[Python learning] Regular expressions

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.