我必须将一个字符串传递到程序中,根据字符串的不同,它只返回一个响应值。我在为两个案例构建模式时遇到困难。
下面是两个示例字符串,它们是四个独立的模式-
phrase1 = "Simple String with some UPPercase in Between ends with?"
phrase2 = "BIG STRING ALL CAPS ENDS WITH?"
phrase3_a = "ALLCAPSSTRING NOTHING AT THE END OF STRING"
phrase3_b = "Any String with ALL UPPERCASE (or not) but ends with!"
phrase4 = "\t\t\t\t"
我还没有建立准确的模式,这就是我要问的。之后我打算用一个
re.compile
所有模式,然后
finditer
使用非“无”组。在下面的代码中,我删除了
whitespaces
,因为如果其他模式都不匹配,则匹配空白模式
[\s]
不退货,我可以单独使用-
phrase=re.sub(r'[\s]','',phrase)
pattern_phrase1 = re.compile (r'[a-zA-Z0-9]\?$')
pattern_phrase2 = re.compile (r'[A-Z0-9]\?$')
pattern_phrase3 = re.compile (r'[A-Z]|[.!$]')