+ 1
Regular Expressions
to remove all last possible patterns ( all exclamation marks) from the end of string, the code is following: but if double exclamation marks, it does not function, where is the mistake in my code? def remove(s): import re for x in s: return re.sub(r'!
#x27;, '', s)10 Respuestas
+ 2
Thanks rodwynnejones! rstrip() method worked!
+ 1
I think you have to add + in the pattern or * instead of $ to remove occurrences of exclamation mark.
+ 1
Give example input and expected output.
You code looks like you iterating through the characters of a string but your returning after the first iteration.
Do you need to use "re"?....if not...have a look at string rstrip() method.
+ 1
thank!
0
Thanks for the answer!
But:
with * instead of $, it will remove all exclamation marks, for example, if they are at the beginning. But I need only from the end of the string.
with add, does no working.
0
Input :[ 'Python !!']
Output : [ 'Python']
0
If you still want to use regex use this pattern:
r"!+quot;
It means "one or more exclamation marks at the end of the string" and should work.
0
what if :
input : „!pyth!on!!!“
output: „python!!!“
removing with regex first and second , but NOT the end?
0
Meka Can you share your edited code?
It's working as expected for me:
import re
input = "„!pyth!on!!!"
print(re.sub(r"!+quot;,"",input))
- 1
Try with the dot with ? Like this
r'!.?#x27;