I want to search for the existence of the word hi
.
import re
word = 'hi?'
cleanString = re.sub('\W+',' ', word)
print(cleanString.lower())
GREETING_INPUTS = ("hello", 'hi', 'hii', "hey")
if cleanString.lower() in GREETING_INPUTS:
print('yes')
else:
print('no')
When word = 'hi'
, it prints yes
. But for word = 'hi?'
, it prints no
. Why is it so and please suggest any solution.
Replace this line:
cleanString = re.sub('\W+',' ', word)
With:
cleanString = re.sub('\W+','', word)
Because you're replacing all the matches of '\\W+'
with ' '
, a space, so the string would be 'hi '
, so then you need to replace it with empty string ''
for it to work, the string would become 'hi'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.