I'm trying to code a script that will find a single word or a string composed of multiple single words in a given string. I've found this answer which looks very much what I'd need, but I can't really understand how it works.
Using the code provided in the answer mentioned above, I have this:
import re
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
st1 = 'those who seek shall find'
st2 = 'swordsmith'
print findWholeWord('seek')(st1) # -> <match object>
print findWholeWord('these')(st1) # -> None
print findWholeWord('THOSE')(st1) # -> <match object>
print findWholeWord('seek shall')(st1) # -> <match object>
print findWholeWord('word')(st2) # -> None
This function returns either something like <_sre.SRE_Match object at 0x94393e0>
(when the word(s) were found) or None
(when they weren't) and I'd like the function to return instead either True
or False
if the word(s) were found or not, respectively.
Since I'm not clear on how the function is working, I'm not sure how I'd do that.
I've never seen a function being called passing two variables (?), ie: findWholeWord(word)(string)
, what is this doing?