regex - Python: Formatting a string with a variable that must be inside curly braces {} -


my task make function regex returns matches count word characters long or longer.

here's tried:

import re  # example: # >>> find_words(4, "dog, cat, baby, balloon, me") # ['baby', 'balloon']  def find_words(count, a_string):     pattern = r'\w{{{},}}'.format(count)     return re.findall(pattern, a_string) 

the reason mess of curly braces attempting escape them.

the final search string (pattern) want \w{count,}

edit: forgot return statement in original post. i'll leave here because answers coming in valuable.

why don't use % operator?

in [1]: def find_words(count, a_string):    ...:     pattern = r'\w{%s,}' % count    ...:     return re.findall(pattern, a_string)  in [2]: find_words(4, "dog, cat, baby, balloon, me") out[2]: ['baby', 'balloon'] 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -