Python:re.sub () Implements string substitution

Source: Internet
Author: User

Tag: regular represents style optional parameter Python lag tin Hello Sub

Features of the 1.re.sub

RE is an abbreviation for regular expression, which indicates regular expressions

A sub is an abbreviation for Substitude, which represents a replacement

Re.sub is a function of regular expressions that implements a more powerful substitution function than a normal string.

eg

Replace vs Re.sub

>>>stri="Hello 111 World 111"

>>>replacestr=stri. Replace ("111", "222") ===> "hello 222 world 222"

If

>>>stri="Hello 123 World 456"

>>>replacedstr = re.sub ("\d+", "222", inputstr) ===> put 123 and 456, switch to 222

Detailed explanations of the various parameters of the 2.re.sub

Re.sub a total of five parameters.

Re.sub (Pattern, Repl, String, count=0, flags=0) Three required parameters: pattern, REPL, string Two optional parameters: count, Flags

Python:re.sub () Implements string substitution

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.