Regular expression | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

Regular expression

Given the quote: "Always do your best. Your best is going to change from moment to moment; it will be different when you are healthy as opposed to sick. Under any circumstance, simply do your best, and you will avoid self-judgment, self-abuse and regret." Complete the program so that it will take a word as input and output the number of times that word appears in the quote. My attempt import re quote = "Always do your best. Your best is going to change from moment to moment; it will be different when you are healthy as opposed to sick. Under any circumstance, simply do your best, and you will avoid self-judgment, self-abuse and regret" r = 'r' gondya = r+'"'+(input())+'"' #your code goes here print(len(re.findall(gondya , quote))) It's not happening Need help

11th May 2021, 7:36 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
10 Respuestas
+ 2
You don't have to use a regular expression to solve this problem. you can solve it simply by iterating and/ or count method. I solved it intwo ways, you can see that here: https://code.sololearn.com/ca4a143A2412
11th May 2021, 8:19 PM
Endalk
Endalk - avatar
+ 3
import re quote = "Always do your best. Your best is going to change from moment to moment; it will be different when you are healthy as opposed to sick. Under any circumstance, simply do your best, and you will avoid self-judgment, self-abuse and regret" userinput = input() print(len(re.findall(userinput, quote)))
10th Jul 2021, 1:18 PM
Small Dog from "2 Stupid Dogs"
Small Dog from "2 Stupid Dogs" - avatar
+ 2
Swapnil Kamdi can you check it out now? I have added another method, i.e, the regular expression as the third solution. https://code.sololearn.com/ca4a143A2412
11th May 2021, 8:45 PM
Endalk
Endalk - avatar
+ 1
Suppose input is "do", so what you are doing results in 'r"do"' and re.findall is probably searching for string 'r"do"' instead of "do" . What you need to do is as follow :- gondya=r""+input() Remove line(r='r')
11th May 2021, 8:03 PM
Abhay
Abhay - avatar
+ 1
Swapnil Kamdi works fine for me and that's why i said to remove line r='r'.Why were you even defining r ? But I think you got the answer !.
11th May 2021, 9:39 PM
Abhay
Abhay - avatar
+ 1
Thanks Abhay I was probably doing mistakes with parenthesis while working with your suggestion.
11th May 2021, 9:42 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
+ 1
word = input() #your code goes here pattern = rf"{word}" print(len(re.findall(pattern, quote)))
31st Jul 2021, 10:15 AM
Constantine Tvalashvili
Constantine Tvalashvili - avatar
0
Have you defined r first. I have tried in both way, it's not working. Please elaborate or post code.
11th May 2021, 8:09 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
0
Thanks for posting Endalk but I knew these methods already. As it has been asked in regular expression hence I'm trying it to solve that way. Could you plz help with resolution that way.
11th May 2021, 8:23 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
0
Thanks Endalk for this, But I could solve it while going through metacharacters. You could put backslash infront of input(). This way, import re quote = "Always do your best. Your best is going to change from moment to moment; it will be different when you are healthy as opposed to sick. Under any circumstance, simply do your best, and you will avoid self-judgment, self-abuse and regret" gondya = input()\ #your code goes here print(len(re.findall(gondya, quote))) Thanks a lot!!
11th May 2021, 8:49 PM
Swapnil Kamdi
Swapnil Kamdi - avatar