Open In App

re.subn() in Python

Last Updated : 30 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

re.subn() method in Python is used to search for a pattern in a string and replace it with a new substring. It not only performs the replacement but also tells us how many times the replacement was made. We can use this method when we need to replace patterns or regular expressions in text and get a count of how many changes were made. We can do this using the re.subn() method in the re module.

Let's understand this with the help of example:

Python
import re

# Original string
s = "Geeks for Geeks"

# Replace 'cats' with 'dogs'
new_s, count = re.subn("nerds", "geeks", s)

print(new_s)
print("Replacements made:", count)

Output
Geeks for Geeks
Replacements made: 0

Syntax of re.subn() in Python

The syntax for the re.subn() method is as follows:

re.subn(pattern, repl, string, count=0, flags=0)

Parameters:

  • pattern: This is the regular expression or pattern that we want to search for in the string.
  • repl: This is the replacement string or a function that will replace the matched pattern.
  • string: This is the original string where the search and replacement will take place.
  • count (optional): This is the maximum number of replacements to make. The default value is 0, which means "replace all occurrences."
  • flags (optional): This is used to modify the behavior of the pattern matching, like re.IGNORECASE, re.MULTILINE, etc.

Return Value:

  • The modified string with the replacements.
  • The number of replacements made.

Using Regular Expressions with re.subn()

We can also use regular expressions to replace patterns that match more complex rules, not just fixed strings. This code uses a regular expression to find phone numbers in a specific format (xxx-xxx-xxxx) and replaces them with a placeholder XXX-XXX-XXXX. The code also prints how many replacements were made.

Python
import re

# Original string
s = "My phone number is 123-456-7890 and my friend's is 987-654-3210"

# Replace all phone numbers (pattern: digits followed by hyphens)
new_s, count = re.subn(r"\d{3}-\d{3}-\d{4}", "XXX-XXX-XXXX", s)

print(new_s)
print(count)

Output
My phone number is XXX-XXX-XXXX and my friend's is XXX-XXX-XXXX
2

Case-Insensitive Replacement

If we want to replace words regardless of their case, we can use the re.IGNORECASE flag. This code uses a regular expression to find occurrences of the word "test", regardless of case.

Python
import re

# Original string
s = "This is a Test. This is another Test."

# Replace 'test' with 'exam', ignoring case
new_s, count = re.subn(r"test", "exam", s, flags=re.IGNORECASE)

print(new_s)
print("Replacements made:", count)

Output
This is a exam. This is another exam.
Replacements made: 2

Replacing HTML Tags

This example replaces all HTML tags with the placeholder [TAG] and counts how many tags were replaced. This code is useful for sanitizing or cleaning HTML tags.

Python
import re

text = "<div>Hello</div> <p>World</p>"

pattern = r"</?\w+>"  # Match HTML tags
replacement = "[TAG]"

modified_text, count = re.subn(pattern, replacement, text)

print(modified_text)
print(count)

Output
[TAG]Hello[TAG] [TAG]World[TAG]
4

Next Article
Article Tags :
Practice Tags :

Similar Reads