Python regular Expressions (ii)

Source: Internet
Author: User

The sub () and Subn () sub (pattern,repl,string,count=0) are used to implement the search and replace function, where the pattern of all regular expressions is replaced with REPL, and all occurrences are replaced unless count is defined. The part used to replace is usually a string, or it may be a function that returns a string. The sub () also returns the total number of replacements that occurred, and returns the and replaced strings as two element tuples.

m = Re.sub ('T','T',' this and that')Print1, MM= Re.subn ('[AE]','X','abcdef')Print2, MD= Re.sub (r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4})'R'\2/\1/\3','November/21st/1995')Print3, D1 This and that2 ('xbcdxf', 2)3 November/21st/1995

Split () Split string:

Re.split (':','str1:str2:str3') ['  str1"str2"str3"

Python regular Expressions (ii)

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.