The first argument to re.sub is treated as a regular expression, so the square brackets get a special meaning and don't match literally.
You don't need a regular expression for this replacement at all though (and you also don't need the loop counter i):
for name_line in name_lines:
text = text.replace(name_line, '')
Answer from Thomas on Stack OverflowThe first argument to re.sub is treated as a regular expression, so the square brackets get a special meaning and don't match literally.
You don't need a regular expression for this replacement at all though (and you also don't need the loop counter i):
for name_line in name_lines:
text = text.replace(name_line, '')
Try to use re.sub to replace the match:
import re
text = """\
Questions and Answers
Operator [1]
Shannon Siemsen Cross, Cross Research LLC - Co-Founder, Principal & Analyst [2]
I hope everyone is well. Tim, you talked about seeing some improvement in the second half of April. So I was wondering if you could just talk maybe a bit more on the segment and geographic basis what you're seeing in the various regions that you're selling in and what you're hearing from your customers. And then I have a follow-up.
Timothy D. Cook, Apple Inc. - CEO & Director [3]"""
text = re.sub(r".*\d]", "", text)
print(text)
Prints:
Questions and Answers
I hope everyone is well. Tim, you talked about seeing some improvement in the second half of April. So I was wondering if you could just talk maybe a bit more on the segment and geographic basis what you're seeing in the various regions that you're selling in and what you're hearing from your customers. And then I have a follow-up.
I am working on a code I found on Github and I am not familiar with regex. Can someone please explain the meaning of this re.sub("^\s*(.-)\s*$", "%1", s).replace("\\n", "\n")
s is the variable containing the string of interest.
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 have the following code as a quick function in a python script to help sync iTunes to an Android phone:
def FixPath(path):return re.sub(r'[^\w_. -]', '', str(path))
However, this doesn't remove periods "." from a string with "...", which causes a problem for file-paths/folder-names in Windows.
I've worked around it by changing the album name that had "..." in it, but is there something I'm missing in the regex that would let it fix this?
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 ('').
As long as you can make do with str.replace(), you should use it. It avoids all the pitfalls of regular expressions (like escaping), and is generally faster.
str.replace() should be used whenever it's possible to. It's more explicit, simpler, and faster.
In [1]: import re
In [2]: text = """For python 2.5, 2.6, should I be using string.replace or re.sub for basic text replacements.
In PHP, this was explicitly stated but I can't find a similar note for python.
"""
In [3]: timeit text.replace('e', 'X')
1000000 loops, best of 3: 735 ns per loop
In [4]: timeit re.sub('e', 'X', text)
100000 loops, best of 3: 5.52 us per loop
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')
I want to replace different patterns differently.