Capture all consecutive all-caps words with regex in python?

后端 未结 4 952
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-10 20:08

I am trying to match all consecutive all caps words/phrases using regex in Python. Given the following:

    text = \"The following words are ALL CAPS. The follow         


        
4条回答
  •  生来不讨喜
    2021-02-10 20:48

    Keeping your regex, you can use strip() and filter:

    string = "The following words are ALL CAPS. The following word is in CAPS."
    result = filter(None, [x.strip() for x in re.findall(r"\b[A-Z\s]+\b", string)])
    # ['ALL CAPS', 'CAPS']
    

提交回复
热议问题