Simple Python Regex Find pattern

后端 未结 5 806
梦谈多话
梦谈多话 2020-12-29 10:41

I have a sentence. I want to find all occurrences of a word that start with a specific character in that sentence. I am very new to programming and Python, but from the li

5条回答
  •  轮回少年
    2020-12-29 11:24

    I second the Dive Into Python recommendation. But it's basically:

    m = re.findall(r'\bf.*?\b', 'a fast and friendly dog')
    print(m)
    

    \b means word boundary, and .*? ensures we store the whole word, but back off to avoid going too far (technically, ? is called a lazy operator).

提交回复
热议问题