I'm learning about regex. If I want to find all the 5 letter words in a string, I could use:
import re
text = 'The quick brown fox jumps over the lazy dog.'
print(re.findall(r"\b[a-zA-z]{5}\b", text))
But I want to write a simple function whose argument includes the string and the length of the word being found. I tried this:
import re
def findwords(text, n):
return re.findall(r"\b[a-zA-z]{n}\b", text)
print(findwords('The quick brown fox jumps over the lazy dog.', 5))
But this returns an empty list. The n
is not being recognized.
How can I specify an argument with the number of repetitions (or in this case, the length of the word)?