Reputation: 11776
So far I have this code:
def find_words(m_count, m_string):
m_list = re.findall(r'\w{6,}', m_string)
return m_list
is there a way to use m_count
instead of using the count number (6) explicitly??
Upvotes: 1
Views: 147
Reputation: 791
Convert the int to string and added to you reg exp like this:
def find_words(m_count, m_string):
m_list = re.findall(r'\w{'+str(m_count)+',}', m_string)
return m_list
Upvotes: 2
Reputation: 3525
You can use format()
, and escape the curly braces.
>>> import re
>>> m_count = 6
>>> print re.findall(r'\w{{{},}}'.format(m_count),'123456789 foobar hello_world')
['123456789', 'hello_world']
Full method body:
def find_words(m_count, m_string):
m_list = re.findall(r'\w{{{},}}'.format(m_count), m_string)
return m_list
Upvotes: 0
Reputation: 785176
You can build a regex by concatenating your count variable and static part like this:
>>> m_count = 6
>>> re.findall(r'\w{' + str(m_count) + ',}', 'abcdefg 1234 124xyz')
['abcdefg', '124xyz']
Upvotes: 1