我需要編寫一個函數,它需要一個count和一個字符串,並返回字符串中所有長度爲或長於字長字符的單詞列表。int在Python中的正則表達式中不起作用def
我的功能是:
import re
def find_words(count, a_str):
count = int(count)
return re.findall(r'\w{},'.format(int(count)), a_str)
但它不工作,這是返回空列表:
例子:
find_words(4, "dog, cat, baby, balloon, me")
應返回:
['baby', 'balloon']
謝謝,但爲什麼需要{ {{}}}而不僅僅是{{}}? –
@Dmitriy_kzn文檔說:「如果您需要在文字中包含大括號字符,則可以通過加倍:'{{'和'}}'來逃脫。 [「格式字符串語法」](https://docs.python.org/3/library/string.html#format-string-syntax)並且你想在正則表達式的文本文本中保留一個大括號字符,但你也想要插入'count'。所以'{{'所以它保留'{'和'''格式化。 – MSeifert