The re.sub method doesn't replace a string in place. It can't, since strings in Python are immutable. When you do a substitution on a string, it returns a new string with the requested changes (or the original string if there was no match). You're currently ignoring the return value, so your code has no effect.
But I don't think you actually need regular expressions at all for this issue. If you want to replace any string that mentions the word super anywhere with the string "superintendent", you can use a simple substring test:
for i, item in enumerate(list_of_strings):
if "super" in item:
list_of_strings[i] = "superintendent"
This will of course be more prone to false-positives than using your current regex. You could still use the structure of the code above with a regex search if you want (just change the if "super" in item: line to if re.search(pattern, item): after setting pattern to a regex that matches the strings you want it to).
python - Replacing entire string if there is a matching word - Stack Overflow
python regex sub replace the whole string - Stack Overflow
Python regex replace whole string - Stack Overflow
python - Search and replace with "whole word only" option - Stack Overflow
The re.sub method doesn't replace a string in place. It can't, since strings in Python are immutable. When you do a substitution on a string, it returns a new string with the requested changes (or the original string if there was no match). You're currently ignoring the return value, so your code has no effect.
But I don't think you actually need regular expressions at all for this issue. If you want to replace any string that mentions the word super anywhere with the string "superintendent", you can use a simple substring test:
for i, item in enumerate(list_of_strings):
if "super" in item:
list_of_strings[i] = "superintendent"
This will of course be more prone to false-positives than using your current regex. You could still use the structure of the code above with a regex search if you want (just change the if "super" in item: line to if re.search(pattern, item): after setting pattern to a regex that matches the strings you want it to).
I'm not sure if I understand your question but if you want to replace every element with the word super in it by superintendant, here's what I would do.
for index,element in enumerate(listToCheck):
if "super" in element:
listToCheck[index]="superintendant"
By the way don't name your variables list because it's a reserved python keyword.
As stated in the docs, the matched part is replaced. Matched is different from captured.
You will have to capture the text you don't want to remove in a capture group like so:
(^/en/category.*)-\d{1,4}$
and put it back into the string using the backreference \1:
re.sub(r'(^/en/category.*)-\d{1,4}$', r'\1', text)
(?<=^\/en\/category)(.*)-\d{1,4}$
Try this.replace by \1.See demo.
https://regex101.com/r/tX2bH4/27
Your whole pattern matches that is why it is replacing the whole string.
P.S match is different than captures or groups.
import re
p = re.compile(r'(?<=^\/en\/category)(.*)-\d{1,4}$', re.IGNORECASE)
test_str = "/en/category/specials/men-2610"
subst = "\1"
result = re.sub(p, subst, test_str)
You want a regular expression. You can use the token \b to match a word boundary: i.e., \bresult\b would match only the exact word "result."
import re
with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
for l in f:
s = l.split('*')
editor = re.sub(r"\b%s\b" % s[0] , s[1], editor)
Use re.sub:
replacements = {'the':'a',
'this':'that'}
def replace(match):
return replacements[match.group(0)]
# notice that the 'this' in 'thistle' is not matched
print re.sub('|'.join(r'\b%s\b' % re.escape(s) for s in replacements),
replace, 'the cat has this thistle.')
Prints
a cat has that thistle.
Notes:
All the strings to be replaced are joined into a single pattern so that the string needs to be looped over just once.
The source strings are passed to
re.escapeto make avoid interpreting them as regular expressions.The words are surrounded by
r'\b'to make sure matches are for whole words only.A replacement function is used so that any match can be replaced.