Python regular substitution string function Re.sub usage example (1)

Source: Internet
Author: User

The examples in this paper describe Python is replacing the string function re.sub usage. Share to everyone for your reference, as follows:

Python re.sub is a standard library of Python regular, with the main function being to match strings to be replaced with regular matches
and replace it with the string you want.
The Re.sub function takes a regular expression-based substitution work

Here is a sample source code

1 #!/usr/bin/env python2 #Encoding:utf-83 ImportRe4URL ='https://113.215.20.136:9011/113.215.6.77/c3pr90ntcya0/youku/6981496DC9913B8321BFE4A4E73/ 0300010e0c51f10d86f80703baf2b1adc67c80-e0f6-4ff8-b570-7dc5603f9f40.flv'5Pattern = Re.compile (r'(? <![ \.\D]) (?: \ D{1,3}\.) {3}\d{1,3} (?! [\.\d])')6 Printpattern.findall (URL)7out = Re.sub (pattern,'127.0.0.1', URL)8 PrintOut

The results of the implementation are as follows:

1 [[email protected] shu]#2 ['113.215.20.136'  113.215.6.77']3 https://127.0.0.1:9011/127.0.0.1/c3pr90ntcya0/ youku/6981496dc9913b8321bfe4a4e73/0300010e0c51f10d86f80703baf2b1adc67c80-e0f6-4ff8-b570-7dc5603f9f40.flv

########### Re.sub Module Detailed

Command: Re.sub (pattern, Repl, String, count=0, flags=0)

The re.sub is used to replace occurrences of strings. If no rule is matched, the original string does not change.

First parameter: rule
Second parameter: replaced string
The third argument: a string
Fourth parameter: Number of replacements. The default is 0, which means that each match is replaced

Python regular substitution string function Re.sub usage example (1)

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.