Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions re.findall()

Tags:

python

regex

I have a function which takes a count and a string as input. It should return a list of all words in that string of length count, and greater. Python however doesn't recognise my variable and returns an empty list.

def word_number(count, string):
    return re.findall(r'\w{count,}', string)

How do I pass in the variable 'count' so that the function returns words of count and longer?

like image 516
fortune Avatar asked Nov 24 '25 19:11

fortune


1 Answers

You can use printf style formatting:

re.findall(r'\w{%s,}' % count, string)
like image 119
heemayl Avatar answered Nov 27 '25 09:11

heemayl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!