Skip to main content
 首页 » 编程设计

python之正则表达式:补充一组字符 (Python)

2025年05月04日23jillzhang

我想编写一个正则表达式来检查单词是否以 s、x、y、z、ch、sh 或元音后跟 s 以外的任何结尾。这是我失败的尝试:

re.match(r".*[^ s|x|y|z|ch|sh|a|e|i|o|u]s",s) 

补一组字符的正确方法是什么?

请您参考如下方法:

使用 str.endswith 的非正则表达式解决方案:

>>> from itertools import product 
>>> tup = tuple(''.join(x) for x in product(('s','x','y','z','ch','sh'), 's')) 
>>> 'foochf'.endswith(tup) 
False 
>>> 'foochs'.endswith(tup) 
True