This question already has answers here:
What exactly is a "raw string regex" and how can you use it?
(7 answers)
Closed 6 years ago.
I am trying to remove tags in text that are identified by a backslash. For example, for the phrase 'Hello \tag world', I'd like to return the phrase 'Hello world'. I've tried the following but it doesn't get rid of the '\tag'.
print re.sub('\\[A-Za-z]+',' ',text)
I'm sure it's something simple, but I can't seem to figure it out.
Thanks for any help you can give!
Must be:
re.sub('\\\\[A-Za-z]+',' ',text)
Otherwise, '\\' is treated as a regex special escape character.
Related
This question already has answers here:
How can I print a single backslash?
(4 answers)
Closed 2 years ago.
I use print() function to use like this:print("\")and i get an exception. Tell me how to slove it. Thks
Use \\ instead.
Actually, \ is a special character and you have to escape it.
print("\\") # print a single "\" character
Use:
print("\\")
instead of print("\")
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Remove specific characters from a string in Python
(26 answers)
Closed 2 years ago.
s = "Bob hit a ball!, the hit BALL flew far after it was hit."
I need to get rid of the following characters from s
!?',;.
How to achieve this with re.sub?
re.sub(r"!|\?|'|,|;|."," ",s) #doesn't work. And replaces all characters with space
Can someone tell me what's wrong with this?
The problem is that . matches all characters, not the literal '.'. You want to escape that also, \..
But a better way would be to not use the OR operator |, but simply use a character group instead:
re.sub(r"[!?',;.]", ' ', s)
This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 4 years ago.
I would like to delete some characters into a string (text with several words).
For this purpose I use the code below:
text2 = text.replace(',', '').replace('\n', '').replace('.', '').replace(':', '')
I have to add one replace method for each character to replace.
Is there any form to code in a smarter way? Something like this:
text2 = text.replace(',' '.' '\n' ':', '') # of course this is not working, just an example about how it could be.
Thank you
use re.sub(r'[,.:\n]', '', text)
This question already has answers here:
Does Python have a string 'contains' substring method?
(10 answers)
Closed 5 years ago.
For example if a string contains:
odfsdlkfn dskfThe Moonaosjfsl dflkfn
How can I check to see if it contains "The Moon"?
What I have currently been doing is (but does not work):
if string.find("The Moon")!=-1:
doSomething
Is there anyway to do this?
Thanks!
Simple:
string = 'odfsdlkfn dskfThe Moonaosjfsl dflkfn'
if 'The Moon' in string:
dosomething
you could use regular expressions:
import re
text = "odfsdlkfn dskfThe Moonaosjfsl dflkfn"
if re.find("The Moon", text):
...
and in this case, you could ingore casing with re(pattern, text, re.IGNORECASE) if needed.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Substitute multiple whitespace with single whitespace in Python
trying to figure out how to write a regex that given the string:
"hi this is a test"
I can turn it into
"hi this is a test"
where the whitespace is normalized to just one space
any ideas? thanks so much
import re
re.sub("\s+"," ",string)
Does it need to be a regex?
I'd just use
new_string = " ".join(re.split(s'\s+', old_string.strip()))
sed
sed 's/[ ]\{2,\}/ /g'