You can use re.sub:
>>> import re
>>> text = "<a> text </a> <c> code </c>"
>>> new_text = re.sub(r'<c>.*?</c>', '', text)
>>> new_text
<a> text </a>
Answer from Sash Sinha on Stack Overflowregex - How to use re.sub in Python? - Stack Overflow
Regular Expressions re.sub
Module re: add possibility to return matches during substitution
Python re.sub() regex to substitute a character with a string except in quotes
Videos
You can use re.sub:
>>> import re
>>> text = "<a> text </a> <c> code </c>"
>>> new_text = re.sub(r'<c>.*?</c>', '', text)
>>> new_text
<a> text </a>
import re
text = "<a> text </a> <c> code </c>"
rg = r"<c>.*<\/c>"
for match in re.findall(rg, text):
text = text.replace(match, "")
Hi. I am currently building my very first python project. I need to get rid of all the Characts inside and including the <>. In the following example, the text "like the side of a house" should be left over.
<span class="idiom_proverb">like the side of a <strong class="tilde">house
</strong> </span>
I want to use re.sub with regular expressions on this one and my current code looks like this:
new_text = re.sub(r"(?:\<.*\>)*", "", old_text)
The problem is, when using re.sub this way, I am essentially deleting everything between the first < and the very last >, loosing all the information in between. Is there a way to make sure that my regular expressions refer to the very next > once a < is found?
This way it would only replace <spand class="idiom\_proverb">, <strong class="tilde">, </strong> and </span>?
I would really appreciate any help. Thank you!
I'm trying to replace the @ character in a line with a string. However, this replacement must NOT occur within quotes, '' AND "".
For example, '@ foo@ "abc@google.com"' replaced with "bar" should become 'bar foobar "abc@google.com"'.
My attempts have got me to : re.sub(r"@(?=([^\"]*\"[^\"]*\")*[^\"]*$)"... but this only works for double quotes (""), not single quotes ('').
You should call group() to get the matching string:
import re
number_mapping = {'1': 'one',
'2': 'two',
'3': 'three'}
s = "1 testing 2 3"
print re.sub(r'\d', lambda x: number_mapping[x.group()], s)
prints:
one testing two three
To make your function fit with re.sub, you can wrap it with a lambda:
re.sub('pattern', lambda m: myfunction(m.group()), 'text')