Built with Tutify.io

    AIStudyLabs

    Python regex find and replace

    Python regex find and replace

    One of the most common scenarios you will ever encounter as you are using the Python re module is the find-and-replace scenario. Here, you will have to search and find a string and then replace it with a desired string. The method for this task is the re.sub() method.

    In this tutorial,  I will show you how you can use the re.sub() method to find and replace an old string with a new string.

    Here is what you will learn in this tutorial:

    <a id="h-basic-syntax-for-the-re-sub-method"></a>

    Basic Syntax for the re.sub() Method

    Understanding the syntax of any method is an essential skill for any developer. In this section, we will get to understand the syntax for the re.sub() method. Below is the syntax:

    
    re.sub(pattern, replacement, text, count=0)
    

    The table below explains every argument in detail:

    | Argument | Description | | --- | --- | | pattern | This is the regular expression pattern that you want to search for in the input text | | replacement | This is the string that will replace the matched pattern in the input text | | text | This is the input text in which you want to perform the find and replace operation | | count(optional) | Specifies the maximum number of replacements to be made in the text |

    <a id="h-using-the-re-sub-method"></a>

    Using the re.sub() Method

    Having looked at the re.sub() method syntax, let us now look at some practical applications of the method.

    <a id="h-literal-find-and-replace"></a>

    Literal Find and Replace

    This is the first practical application of the re.sub() method. Here you might want to do a find-and-replace operation where the arguments pattern and replacement are being taken as normal literal strings without any interpretation associated with them. In such a case,  the way around that is the re.escape() method, it ensures that any special characters are treated as literal strings during the find and replace operation. Let us look at the following example:

    
    import re
    
    text = "Any course on AIStudyLabs.com website costs $100."
    
    # escaping the pattern
    pattern = re.escape("$100")
    
    # declaring the string for replacement
    replacement = "€50"
    
    # finding and replacing the string
    new_text = re.sub(pattern, replacement, text)
    print(new_text)
    

    In the code above, we have a text variable that holds the original string we want to modify. The pattern we want to search for is "$100", which represents the cost of a course. Since "$" is a special character in regular expressions, we need to escape it using re.escape() to treat it as a literal character.

    The replacement variable holds the string we want to replace "$100" with, which is "€50" representing a different currency.

    Using re.sub(), we perform the find and replace operation by passing the pattern, replacement, and text as arguments. The resulting updated text is assigned to the new_text variable.

    Output:

    
    #Output
    Any course on AIStudyLabs.com website costs €50.
    

    <a id="h-replacing-with-literal-strings"></a>

    Replacing with Literal Strings

    In situations where the pattern has no special characters,  you can just go ahead and use a normal literal as in the example below:

    
    import re
    
    text = "I love learning programming on AIStudyLabs.com"
    
    # pattern to search for 
    pattern = "AIStudyLabs.com"
    
    # replacement string
    replacement = "AIStudyLabs.edu"
    
    # performing the find and replace operation
    new_text = re.sub(pattern, replacement, text)
    
    # printing the modified text
    print(new_text)
    

    In the code above, we have a string text containing the original text where we want to perform the find and replace operation.

    We define the pattern variable to store the literal string we want to search for, in this case, "AIStudyLabs.com". Since we are searching for a literal string, we don't need to use regular expression syntax or escape any special characters.

    The replacement variable contains the literal string that will replace the matched pattern. In this example, we are replacing "AIStudyLabs.com" with "AIStudyLabs.edu".

    Using the re.sub() method, we perform the find and replace operation.

    Output:

    
    #Output
    I love learning programming on AIStudyLabs.edu
    

    <a id="h-replacing-all-occurrences"></a>

    Replacing All Occurrences

    You might also want to replace all occurrences of a pattern in a string. To do that simply include the count argument and set it to 0. Here is an example:

    
    import re
    
    text = "I love programming. I speak programming. I learn programming on AIStudyLabs.com and programming is my passion."
    
    # Pattern to search for
    pattern = "programming"
    
    # Replacement string
    replacement = "coding"
    
    # Perform the find and replace operation, replacing all occurrences
    new_text = re.sub(pattern, replacement, text, count=0)
    
    # Print the modified text
    print(new_text)
    

    In this example, the text is "I love programming. I speak programming. I learn programming on AIStudyLabs.com and programming is my passion." The pattern to search for is "programming", and the replacement string is "coding".

    By calling re.sub(pattern, replacement, text, count=0), the code performs the find and replace operation, replacing all occurrences of the pattern "programming" with the replacement string "coding". Since the count parameter is set to 0, all occurrences are replaced.

    The modified text is then stored in the new_text variable.

    Output:

    
    #Output
    I love coding. I speak coding. I learn coding on AIStudyLabs.com and coding is my passion.
    

    <a id="h-limiting-the-number-of-replacements"></a>

    Limiting the Number of Replacements

    To limit the number of replacements just set the count argument to any positive integer. Here is an example:

    
    import re
    
    text = "I love programming. I speak programming. I learn programming on AIStudyLabs.com and programming is my passion."
    
    # pattern to search for
    pattern = "programming"
    
    # replacement string
    replacement = "coding"
    
    # perform the find and replace operation, replacing all occurrences
    new_text = re.sub(pattern, replacement, text, count=2)
    
    # print the modified text
    print(new_text)
    

    The example is the same one we used when replacing all occurrences, but here the count argument is set to 2, limiting the replacements to only two occurrences. Therefore, the first two occurrences of "programming" in the text will be replaced with "coding".

    Output:

    
    #Output
    I love coding. I speak coding. I learn programming on AIStudyLabs.com and programming is my passion.
    

    <a id="h-conclusion"></a>

    Conclusion

    That’s it from this tutorial, we hope there is so much that you have learned about the find and replace using the re.sub() method in re module.