Python3中替换指定字符:replace()和正则表达式

在Python3中替换给出字符串中的指定字符或者特殊字符的方法:

1,用replace()进行替换

2,用正则表达式进行替换

import re


# 1,用字符串本身的replace方法:
print('=======replace()替换=======')
a1 = 'Hello world'
b1 = a1.replace('world', 'python')
print('1原始字符串:{}'.format(a1))
print('1替换字符串:{}'.format(b1))

a2 = 'Hello world world world world World'
b2 = a2.replace('world', 'python')
print('2原始字符串:{}'.format(a2))
print('2替换字符串:{}'.format(b2))

a3 = 'Hello world world world world World'
b3 = a3.replace('world', 'python', 2)    # 数字2表示替换2次
print('3原始字符串:{}'.format(a3))
print('3替换字符串:{}'.format(b3))

a4 = 'Hello world world world world World'
b4 = a4.replace('world', 'python', 2).replace('W', 'H').replace(' ', '_')    # replace()可连续使用
print('4原始字符串:{}'.format(a4))
print('4替换字符串:{}'.format(b4))

print('*****替换特殊字符*****')
a5 = 'Hello\world\hello/python:World*Python?555"666<777>888|999OK'
b5 = a5.replace('\\', '_')    # 注意\\表示\
print('5原始字符串:{}'.format(a5))
print('5替换字符串:{}'.format(b5))

b6 = a5.replace('/', '_')
print('6原始字符串:{}'.format(a5))
print('6替换字符串:{}'.format(b6))
b7 = a5.replace(':', '_').replace('"', '_')
print('7原始字符串:{}'.format(a5))
print('7替换字符串:{}'.format(b7))

print('=======正则表达式替换=======')
# 2用正则表达式来完成替换:
c1 = 'hello world'
strinfo = re.compile('world')
d1 = strinfo.sub('python', c1)
print('1原始字符串:{}'.format(c1))
print('1替换字符串:{}'.format(d1))

c2 = 'hello world world world World'
strinfo = re.compile('world')
d2 = strinfo.sub('python', c2,)
print('2原始字符串:{}'.format(c2))
print('2替换字符串:{}'.format(d2))

c2_1 = 'hello world world world World'
strinfo = re.compile('world')
d2_1 = strinfo.sub('python', c2_1, 2)    # 只替换2次
print('2_1原始字符串:{}'.format(c2_1))
print('2_1替换字符串:{}'.format(d2_1))

c2_2 = 'hello world world world World'
strinfo = re.compile('world', re.I)   # re.I 表示忽略大小写
d2_2 = strinfo.sub('python', c2_2)
print('2_2原始字符串:{}'.format(c2_2))
print('2_2替换字符串:{}'.format(d2_2))

print('*****替换特殊字符*****')
c3 = 'Hello-world\hello/python:World*Python?555"666<777>888|999OK'
strinfo = re.compile('[/:*?"<>|\\\\]')    # 注意用4个\\\\来替换\
d3 = strinfo.sub('_', c3)
print('3原始字符串:{}'.format(c3))
print('3替换字符串:{}'.format(d3))

c4 = 'Hello-world\hello/python:World*Python?555"666<777>888|999OK'
strinfo = re.compile(r'[/:*?"<>|\\]')    # 加r,2个\即可
d4 = strinfo.sub('_', c4)
print('4原始字符串:{}'.format(c4))
print('4替换字符串:{}'.format(d4))

替换结果:

=======replace()替换=======
1原始字符串:Hello world
1替换字符串:Hello python
2原始字符串:Hello world world world world World
2替换字符串:Hello python python python python World
3原始字符串:Hello world world world world World
3替换字符串:Hello python python world world World
4原始字符串:Hello world world world world World
4替换字符串:Hello_python_python_world_world_Horld
*****替换特殊字符*****
5原始字符串:Hello\world\hello/python:World*Python?555"666<777>888|999OK
5替换字符串:Hello_world_hello/python:World*Python?555"666<777>888|999OK
6原始字符串:Hello\world\hello/python:World*Python?555"666<777>888|999OK
6替换字符串:Hello\world\hello_python:World*Python?555"666<777>888|999OK
7原始字符串:Hello\world\hello/python:World*Python?555"666<777>888|999OK
7替换字符串:Hello\world\hello/python_World*Python?555_666<777>888|999OK
=======正则表达式替换=======
1原始字符串:hello world
1替换字符串:hello python
2原始字符串:hello world world world World
2替换字符串:hello python python python World
2_1原始字符串:hello world world world World
2_1替换字符串:hello python python world World
2_2原始字符串:hello world world world World
2_2替换字符串:hello python python python python
*****替换特殊字符*****
3原始字符串:Hello-world\hello/python:World*Python?555"666<777>888|999OK
3替换字符串:Hello-world_hello_python_World_Python_555_666_777_888_999OK
4原始字符串:Hello-world\hello/python:World*Python?555"666<777>888|999OK
4替换字符串:Hello-world_hello_python_World_Python_555_666_777_888_999OK

Process finished with exit code 0

正则替换表达式的另一种写法:

http://www.runoob.com/python/python-reg-expressions.html

re.sub(pattern, repl, string, count=0, flags=0)
  • pattern : 正则中的模式字符串。
  • repl : 替换的字符串,也可为一个函数。
  • string : 要被查找替换的原始字符串。
  • count : 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配。
  • flags : 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。

参考:

https://www.cnblogs.com/wanpython/p/3250528.html

http://www.runoob.com/python3/python3-string-replace.html

https://blog.csdn.net/IAlexanderI/article/details/79460909?utm_source=blogxgwz4

https://www.jianshu.com/p/4c076da1b7f7

https://blog.csdn.net/jesszen/article/details/80945755

https://www.cnblogs.com/wangyongbin/p/4253805.html

https://blog.csdn.net/Gun_1986/article/details/80422339?utm_source=blogxgwz7

https://www.cnblogs.com/summerkiki/p/4488053.html

 

 

  • 16
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值