我需要一些关于字符串正则表达式匹配句子中的单词的帮助,包括标点符号和行尾。我对行尾情况的尝试失败。
以下示例根据我的需要进行评估:
>>> print bool(re.search('test[^a-z]','test!'.lower()))
True
>>> print bool(re.search('test[^a-z]','test aaa'.lower()))
True
>>> print bool(re.search('test[^a-z]','testaaa'.lower()))
False
然而,行尾计算结果为 False
:
>>> print bool(re.search('test[^a-z]','test'.lower()))
False
行尾字符 $
不在集合 a-z
中,所以是本地的 我认为这种情况也会评估 True
。我如何在 regex
中处理这个问题?
最佳答案
关于python - 正则表达式匹配 [^a-z] 或 $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13159426/