replace regex in string python code example
Example 1: python replace regex
import re
s = "Example String"
replaced = re.sub('[ES]', 'a', s)
print replaced
# will print 'axample atring'
Example 2: replace string in typescript
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
var newstr = str.replace(re, "oranges");
console.log(newstr)
Example 3: python replace char in string
# Python3 program to demonstrate the
# use of replace() method
string = "geeks for geeks geeks geeks geeks"
# Prints the string by replacing geeks by Geeks
print(string.replace("geeks", "Geeks"))
# Prints the string by replacing only 3 occurrence of Geeks
print(string.replace("geeks", "GeeksforGeeks", 3))
Example 4: python regex substitute
import re
s = "Example String"
replaced = re.sub('[ES]', 'a', s)
print replaced
Example 5: str replace python regex
import re
line = re.sub(r"</?\[\d+>", "", line)
# Comented version
line = re.sub(r"""
(?x) # Use free-spacing mode.
< # Match a literal '<'
/? # Optionally match a '/'
\[ # Match a literal '['
\d+ # Match one or more digits
> # Match a literal '>'
""", "", line)
Example 6: replacing a value in string using aregular expression pyhton
import re
s = '[email protected] [email protected] [email protected]'
print(re.sub('[a-z]*@', 'ABC@', s))
# ABC@xxx.com ABC@yyy.com ABC@zzz.com