Python Regex: How To Replace All Substrings In a String

preview_player
Показать описание
↓ Code Available Below! ↓

If you find this video useful, like, share and subscribe to support the channel!

Code used in this Python Code Clip:

import re

lines = '''Nappa - What does the scouter say about his power level?
Vegeta - It's over 9000!
Nappa - What 9000? That can't be right. Can it?'''

repl = "8000",
string = lines)

print(new_lines)

* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .

Рекомендации по теме
Комментарии
Автор

Code used in this video:

import re

lines = '''Nappa - What does the scouter say about his power level?
Vegeta - It's over 9000!
Nappa - What 9000? That can't be right. Can it?'''

# Use re.sub() to find and replace substrings
new_lines = re.sub(pattern = "[0-9]+",
repl = "8000",
string = lines)

print(new_lines)

DataDaft
Автор

Thanks a lot. You deserve more than one like.

KhalilYasser