Python regex to match punctuation at end of string

后端 未结 3 1796
伪装坚强ぢ
伪装坚强ぢ 2021-02-15 17:40

I need to match if a sentence starts with a capital and ends with [?.!] in Python.

EDIT It must have [?.!] only at end but allo

3条回答
  •  走了就别回头了
    2021-02-15 17:48

    I made it working for myself, and just for clarification or if other people have the same problem this is what did the trick for me:

    re.match('^[A-Z][^?!.]*[?.!]$', sentence) is not None
    

    Explanation: Where ^[A-Z] looks for a Capital at start

    '[^?!.]*' means everything in between start and end is ok except things containing ? or ! or .

    [?.!]$ must end with ? or ! or .

提交回复
热议问题