Introduction to Python regex replace
In this article, we are discussing regular expression in Python with replacing concepts. In Python, strings can be replaced using replace() function, but when we want to replace some parts of a string instead of the entire string, then we use regular expressions in Python, which is mainly used for searching and replacing the patterns given with the strings that need to be replaced. In Python, regular expressions use the “re” module when there is a need to match or search or replace any part of a string with some specified pattern. So before searching or replacing any string, we should know how to write regular expressions with known Meta characters that are used for writing patterns.
Functions of Python regex replace
In this article, we are discussing how regular expression is used for replacing string or substring. In Python, a common task is to find and replace a part of a string with some specified patterns using regular expressions, so to do this, we have to use the sub() method. This is one of the most important methods in the re module that uses regular expressions. In python, there are provided with meta characters to write regular expressions. Some of the metacharacters are given as below:
1. + this symbol is used for matching one or more occurrences of preceding expressions.
2. ? this symbol is used for matching zero or one occurrence of preceding expressions.
3. . this is used to match any single character except a newline.
4. * this symbol is used to match zero or more occurrences of preceding expressions
5. ^ this is used to match the starting of the line.
6. $ this is used to match the end of the line.
This sub() method takes input as a pattern as substring to replace and substring to replace with and the original string that needs to be modified. Let us take a simple example below.
Example of Python regex replace
Below is an example of Python regex replace:
import re print("The simple program for sub() method for replacing:") str = "Educba#!!Training#!!Institute" print("The given string that needs to be replaced with given pattern is as follows:") print(str) res_str = re.sub("#!!"," ",str) print("The string obtained after replacing special character with space is as follows:") print (res_str)
Explanation: In the above program, we can see that we have a simple string with some special characters in it. str = Educba#!!Training#!!Institute this string needs to be modified with the string that should contain only the string and not special characters. So we need to replace special characters “#!!” with white space so that the given string is readable.
Now let us the proper syntax and example of the sub()method below.
re.sub( pattern, replc, string, max = 0)
Parameters of Python regex replace
Below are the parameters of Python regex replace:
- pattern: In this, we write the pattern to be searched in the given string.
- replc: This parameter is for replacing the part of the string that is specified.
- string: This provides a string that needs to be replaced with a given pattern
- max: This is used to replace all the occurrences until the max number is provided.
This sub() method is used to replace all the given “re” pattern in the string, which is in the replc parameter and substituting all occurrences unless the max value is specified. This method returns the modified string after the replacement of the string is done.
Now let us see an example below that uses this sub() method:
import re print("The below program is used to demonstrate sub() method:") phonenum = "2004-959-559 # This is Phone Number" print("The given string is as follows:") print(phonenum) correct_num1 = re.sub(r'#.*$', "", phonenum) print("The correct phone number by removing special characters in the given string which means replacing them by space is as follows:") print(correct_num1) correct_num2 = re.sub(r'\D', "", phonenum) print("Now we have replaced all the spaces and have only digits in the given string is as follows:") print(correct_num2)
Explanation: In the above program, we can see that we have a string with a variable name “phonenum”, which we have a string with phone number also, but we need only digits for obtaining proper phone number. So we have first obtained a phone number where we have specified a sub() method where we are replacing the string starting from “#” and everything till the end so we obtain the phone number as “2004-959-559”, but as we know, there should not be any special characters in a phone number as it is a numerical value. Hence again, we use the sub () method, which replaces the entire string to be only digits by using a regular expression as “\D” is replaced by nothing, but it should contain only digits.
The sub() function uses backslash, which has the same concept as in the regular expression for replacement of the text. Hence it would help if you used raw string for replacing the string. Suppose let us consider if we want to c: \home as a replacement string, then we specify the expression in the sub() method as r”c:\\home”.
In this article, we saw what a regular expression is and why are they used when we want to replace any part of a string with a specified pattern. In Python, we use replace() function in the string concept, and it cannot be used for replacing the substring, or part of string were to replace() function is used to replace the entire string; hence to do this, we use regular expression which provides “re” module and to replace part of the string we use sub() function as we saw how simple it is to use this function for replacing the given pattern in the actual string to obtain the modified string. This is mainly used for replacing special characters with spaces or some other characters to make the string readable.
This is a guide to Python regex replace. Here we discuss a brief overview of the Python regex replace method and its Examples, along with its Code Implementation. You can also go through our other suggested articles to learn more –