Python回复:如何设置模式重复次数的限制

Python re: how to set a limit to how many times a pattern is repeated

假设我想要一个模式只重复 5 次 我应该如何编写正则表达式模式? 我想让它, 模式重复 1、2、3、4、5 次 returns 正确, 模式重复 6 次或更多次 returns false

pattern = re.compile("[A-Za-z0-9]{3}[,]")
 # This only catches one instance of the pattern

pattern = re.compile("[A-Za-z0-9]{3}[,])+ ")
# This catches one or more instance the pattern




将整个表达式放在括号内,没有。在它之后的花括号中重复。例如,对于 3 个声誉,它将是 - pattern = re.compile("([A-Za-z0-9]{3}[,])){3}")

发现我可以设置一个实例可以重复多少次的下限和上限

模式=re.compile("([A-Za-z0-9]{3}[]){1,5}")