+ 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)

20th Dec 2020, 10:39 PM
Meerim Jeembaeva
Meerim Jeembaeva - avatar
10 ответов
+ 2
Thanks rodwynnejones! rstrip() method worked!
21st Dec 2020, 1:24 AM
Meerim Jeembaeva
Meerim Jeembaeva - avatar
+ 1
I think you have to add + in the pattern or * instead of $ to remove occurrences of exclamation mark.
20th Dec 2020, 10:45 PM
HBhZ_C
HBhZ_C - avatar
+ 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.
21st Dec 2020, 12:00 AM
rodwynnejones
rodwynnejones - avatar
+ 1
thank!
21st Dec 2020, 1:30 AM
Meerim Jeembaeva
Meerim Jeembaeva - avatar
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.
20th Dec 2020, 11:00 PM
Meerim Jeembaeva
Meerim Jeembaeva - avatar
0
Input :[ 'Python !!'] Output : [ 'Python']
21st Dec 2020, 12:39 AM
Meerim Jeembaeva
Meerim Jeembaeva - avatar
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.
21st Dec 2020, 1:29 AM
Kevin ★
0
what if : input : „!pyth!on!!!“ output: „python!!!“ removing with regex first and second , but NOT the end?
21st Dec 2020, 1:55 AM
Meerim Jeembaeva
Meerim Jeembaeva - avatar
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))
21st Dec 2020, 2:00 AM
Kevin ★
- 1
Try with the dot with ? Like this r'!.?
#x27;
20th Dec 2020, 11:05 PM
HBhZ_C
HBhZ_C - avatar