a fair player a fair player - 1 year ago 108
Python Question

Python, regex dynamic count

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
instead of using the count number (6) explicitly??

Answer Source

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']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download