str.replace() v2|v3 does not recognize regular expressions.
To perform a substitution using a regular expression, use re.sub() v2|v3.
For example:
import re
line = re.sub(
r"(?i)^.*interfaceOpDataFile.*$",
"interfaceOpDataFile %s" % fileIn,
line
)
In a loop, it would be better to compile the regular expression first:
import re
regex = re.compile(r"^.*interfaceOpDataFile.*$", re.IGNORECASE)
for line in some_file:
line = regex.sub("interfaceOpDataFile %s" % fileIn, line)
# do something with the updated line
Answer from Andrew Clark on Stack Overflowstr.replace() v2|v3 does not recognize regular expressions.
To perform a substitution using a regular expression, use re.sub() v2|v3.
For example:
import re
line = re.sub(
r"(?i)^.*interfaceOpDataFile.*$",
"interfaceOpDataFile %s" % fileIn,
line
)
In a loop, it would be better to compile the regular expression first:
import re
regex = re.compile(r"^.*interfaceOpDataFile.*$", re.IGNORECASE)
for line in some_file:
line = regex.sub("interfaceOpDataFile %s" % fileIn, line)
# do something with the updated line
You are looking for the re.sub function.
import re
s = "Example String"
replaced = re.sub('[ES]', 'a', s)
print(replaced)
will print axample atring
python - How to input a regex in string.replace? - Stack Overflow
How to replace string using regular expression in Python - Stack Overflow
python .replace() regex - Stack Overflow
String replace method in dart with regex
This tested snippet should do it:
import re
line = re.sub(r"</?\[\d+>", "", line)
Edit: Here's a commented version explaining how it works:
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)
Regexes are fun! But I would strongly recommend spending an hour or two studying the basics. For starters, you need to learn which characters are special: "metacharacters" which need to be escaped (i.e. with a backslash placed in front - and the rules are different inside and outside character classes.) There is an excellent online tutorial at: www.regular-expressions.info. The time you spend there will pay for itself many times over. Happy regexing!
str.replace() does fixed replacements. Use re.sub() instead.
This works.
import re
s3 = ['March/21/2019' , 'Mar/23/2019']
s3 = [re.sub(r'Mar[a-z]*', '03', item) for item in s3]
# ['03/21/2019', '03/23/2019']
Of course, you can also use a for loop for better readability.
import re
s3 = ['March/21/2019' , 'Mar/23/2019']
for i in range(len(s3)):
s3[i] = re.sub(r'Mar[a-z]*', '03', s3[i])
# ['03/21/2019', '03/23/2019']
If you're only working with dates, try something like this:
import datetime
s3 = ['March/21/2019' , 'Mar/23/2019']
for i in range(0, len(s3)):
try:
newformat = datetime.datetime.strptime(s3[i], '%B/%d/%Y')
except ValueError:
newformat = datetime.datetime.strptime(s3[i], '%b/%d/%Y')
s3[i] = newformat.strftime('%m/%d/%Y')
s3 now contains ['03/21/2019', '03/23/2019']
No. Regular expressions in Python are handled by the re module.
article = re.sub(r'(?is)</html>.+', '</html>', article)
In general:
str_output = re.sub(regex_search_term, regex_replacement, str_input)
In order to replace text using regular expression use the re.sub function:
sub(pattern, repl, string[, count, flags])
It will replace non-everlaping instances of pattern by the text passed as string. If you need to analyze the match to extract information about specific group captures, for instance, you can pass a function to the string argument. more info here.
Examples
>>> import re
>>> re.sub(r'a', 'b', 'banana')
'bbnbnb'
>>> re.sub(r'/\d+', '/{id}', '/andre/23/abobora/43435')
'/andre/{id}/abobora/{id}'