我正在使用 python,这个正则表达式不匹配,我不明白为什么。
string = "15++12"
if re.match("[-+*/][-+*/]+",string):
# raise an error here
如果“-”、“+”、“*”、“/”中的一个或多个跟在另一个错误之后,我将尝试引发错误。
最佳答案
使用re.search()
作为re.match()
只搜索字符串的开头:
string = "15++12"
if re.search("[-+*/][-+*/]+",string):
# raise an error here
此外,这可以简化为:
string = "15++12"
if re.search("[-+*/]{2,}",string):
# raise an error here
作为 {2,}
运算符搜索两个或多个先前的类。
关于python - 为什么这个正则表达式不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7138560/