List of Metacharacters:
Before using the pattern to replace string, you have to know the way to write a regular expression pattern. You can use any string value as a pattern for the exact match. But for the specific search, you have to write the regular expression pattern by using metacharacter. The list of the most used metacharacters for writing patterns is given below with meaning.
Character | Description |
---|---|
Β Β Β . | Β It is used to match any single character except a newline. |
Β Β Β ^ | Β It is used to match any character or string at the beginning of the
string. |
Β Β Β $ | Β It is used to match any character or string at the end of the string. |
Β Β Β + | Β It is used to match one or more occurrences of the pattern. |
Β Β Β ? | Β It is used to match zero or one occurrence of the pattern. |
Β Β ( ) | Β It is used for grouping patterns. |
Β { } | Β It is used to match based on lower or upper or both lower and upper
limits. |
Β Β [ ] | Β It is used to match characters based on the given range. |
Β Β | | Β It is used to match patterns based on OR logic. |
Β Β \ | Β It is used to define specific characters or non-characters or digits or
non-digits. |
Replace Method:
sub() method of βreβ module is used in Python for the string replacement.
Syntax:
Here pattern, replace and string are mandatory arguments. If the pattern is matched with any part of the string then it will replace the part by the value of replacing the argument. The other two arguments are optional. Some uses of the above-mentioned metacharacters with sub() method are shown in the following string replacement examples.
Example-1: Replace string by exact match
If you know the exact string value that you want to search in the main string then you can use the searching string value as a pattern in sub() method. Create a python file with the following script. Here, the searching string value is βrainyβ and the replacing string value is βsunnyβ.
# Import regex module
import re
# Define a string
orgStr = "It is a rainy day"
# Replace the string
repStr = re.sub("rainy", "sunny", orgStr)
# Print the original string
print("Original Text:", orgStr)
# Print the replaced string
print("Replaced Text:", repStr)
Output:
The output is shown on the right side of the image.
Example-2: Search and Replace string in the beginning
Create a python file with the following script to know the use of β^β in the regular expression pattern. Here, β^[A-Za-z]+β is used as searching pattern. It will search all alphabetic characters from A to Z and a to z at the beginning of the text and replace it with an empty value. The replaced string will be printed in uppercase for upper() method.
# Import regex module
import re
# Take a string input
originalText = input("Enter a text\n")
# Replace the String based on the pattern
replacedText = re.sub('^[A-Za-z]+' , '', originalText).upper()
# Print the replaced string
print("Replaced Text:", replacedText)
Output:
The output is shown on the right side of the image. Here, βHello, welcome to linuxhintβ is taken as input and βHelloβ word is replaced by β β for the pattern.
Example-3: Search and Replace string at the end
Create a python file with the following script to know the use of β$β symbol in regular expression pattern. Here, β[a-z0-9]+$‘ is used as a pattern in the script. It will search all small alphabets and digits at the end of the text and if returns true then the matching part will be replaced by the string, ‘com.bdβ.
# Import regex module
import re
# Take a string input
originalText = input("Enter a url address\n")
# Replace the String based on the pattern
replacedText = re.sub('[a-z0-9]+$' , 'com.bd', originalText)
# Print the replaced string
print("Replaced Text:", replacedText)
Output:
The output is shown in the right side of the image. Here, βhttps://www.google.com‘ is taken as input text and after replace, ‘https://www.google.com.bdβ is printed as output.
Example-4: Search and Replace the specific part of a string
Create a python file with the following script to search and replace the part of the text in the place where the pattern matches. Here, a list of email addresses is assigned as text into the variable named emails. β@[a-z]β is used pattern for searching. It will search any sub-string starts with small alphabets followed by β@β symbol. If any sub-string matches then it will replace that sub-string by β@linuxhintβ.
# Import regex module
import re
# Define a string
emails = '\n[email protected] \n[email protected] \n[email protected]'
# Replace the specific part of the string based on pattern
replacedText = re.sub('@[a-z]*', '@linuxhint', emails)
# Print the original string
print("Original Text:", emails)
# Print the replaced string
print("\nReplaced Text:", replacedText)
Output:
The output is shown on the right side of the image. Here, every domain part of the email address assigned in the text is replaced by ‘linuxhintβ.
Conclusion:
Some very common uses of the regular expression patterns are shown in this tutorial for string replacement. Many other options exist in python to write different types of simple and complicated patterns for searching and replacing the string of the text.
Watch Author’s Video: here