Home  >  Article  >  Backend Development  >  Python regular replacement string function re.sub usage example

Python regular replacement string function re.sub usage example

高洛峰
高洛峰Original
2017-02-23 15:48:422902browse

The example in this article describes the usage of Python regular replacement string function re.sub. Share it with everyone for your reference. The details are as follows:

python re.sub belongs to the python regular standard library. Its main function is to match the string to be replaced with regular expression
and then replace it. How to create the string you want
re.sub function performs replacement work based on regular expressions

The following is a sample source code

#!/usr/bin/env python
#encoding: utf-8
import re
url = 'https://113.215.20.136:9011/113.215.6.77/c3pr90ntcya0/youku/6981496DC9913B8321BFE4A4E73/0300010E0C51F10D86F80703BAF2B1ADC67C80-E0F6-4FF8-B570-7DC5603F9F40.flv'
pattern = 'http://(.*?):9011/'
out = re.sub(pattern, 'http://127.0.0.1:9091/', url)
print out

The running effect diagram is as follows:

Python regular replacement string function re.sub usage example

For more Python regular replacement string function re.sub usage examples and related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn