2047. 句子中的有效单词数

exiaohu 于 2022-01-27 发布

题目链接:2047. 句子中的有效单词数

如题,用正则表达式描述题中所给规则即可。

import re


class Solution:
    def countValidWords(self, sentence: str) -> int:
        return sum(re.fullmatch(r'([a-z]+(-[a-z]+)?)?[!.,]?', word) is not None for word in sentence.split())