regex sub() function in python by R4R Team

- sub() is a python regular expression function that are used to replace the matches with the new string.

Syntax-

re.sub(pattern,newmatch,string)

Example-

string is s="This is programming language"
re.sub("s","-",s) will return "This-is-programming-language"


program-

import re

print("Enter the string")
s=input()

print("Enter part of string that want to replace")
old=input()

print("Enter something that replace the given match")
new=input()

x=re.sub(old,new,s)

print("nInput string is",s)
print("String After replace is",x)


output-

Enter the string

This-Is-The-String
Enter part of string that want to replace
-
Enter something that replace the given match
_

Input string is: This-Is-The-String
String After replace is: This_Is_The_String


-In this program, we take a input of the original string then take input of choice of replace string, the replace the given match by regular expression.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!