天天看點

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