Python

Python String Replacement using Pattern

Any string data can be replaced with another string in Python by using the replace() method. But if you want to replace any part of the string by matching a specific pattern then you have to use a regular expression. It is used to search a specific pattern in a particular string value and the string will be replaced with another string if any match found. Python uses ‘re’ module to use regular expression pattern in the script for searching or matching or replacing. Using regular expression patterns for string replacement is a little bit slower than normal replace() method but many complicated searches and replace can be done easily by using the pattern. You can replace a string in various ways using the pattern in Python. Some common uses of pattern to replace string are shown in this tutorial. Spyder3 editor is used here to write and run the script.

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:

sub(pattern, replace, string, count=0, flags=0)

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’.

#!/usr/bin/env python3
# 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.

#!/usr/bin/env python3
# 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’.

#!/usr/bin/env python3
# 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’.

#!/usr/bin/env python3
# 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

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.