Python Re.search() And Re.findall()
I am trying to solve this from problem from Hackerrank. It is a Machine Learning problem. Initially, I tried to read all the words from the Corpus file for building unigram frequen
Solution 1:
As I posted on your previous question you should use re.findall()
- but regardless, your problem is that your regex is wrong. See the below example:
>>> import re
>>> regex = re.compile(r'([a-z][a-z-\']+[a-z])')
>>> regex.findall("HELLO W-O-R-L-D") # this has uppercase
[] # there are no results here, because the string is uppercase
>>> regex.findall("HELLO W-O-R-L-D".lower()) # lets lowercase
['hello', 'w-o-r-l-d'] # now we have results
>>> regex.findall("123hello456world789")
['hello', 'world']
As you can see, the reason why you were failing on the first sample you provided is because of the uppercase, you can simply add the re.IGNORECASE
flag, though you mentioned that matches should be lowercase only.
Post a Comment for "Python Re.search() And Re.findall()"