Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Get to Know re.sub()
00:00
In Python, leveraging regex usually means to use the re module. In your particular case, you’ll use re.sub() to substitute a string with a string based on a regex pattern that is part of your arguments.
00:15
re.sub() takes three positional arguments: pattern, repl, and string. re.sub() also takes two optional arguments: count and flags.
00:26
So, what does re.sub() do? re.sub() returns the string obtained by replacing occurrences of pattern in string by the replacement that you pass in as repl.
00:38
If the pattern isn’t found, string is returned unchanged. The optional argument count defines the maximum number of pattern occurrences to be replaced.
00:50 If omitted or zero, all occurrences will be replaced.
00:54
The other optional argument, flags, modifies the regex parsing behavior, allowing you to refine your pattern matching even further. Okay, so much for the theory.
Become a Member to join the conversation.