我想写一个正则表达式,它能够匹配两组数字或100%由'x'字符完全分隔开的情况。如(first group)x(second group)。并且"100%"应按原样捕获,包括百分号符号。只有在100后才允许使用百分号。
例如:
100x100 - OK
100%x100 - OK
99x99 - OK
99%x100 - not OK
99x99% - not OK
这是我的方法:
s = '100%x99%' # this should be failed
match = re.match(r'(100%|\d+(?!%))x(100%|\d+(?!%))', s)
if match:
x = match.group(1)
y = match.group(2)
我希望y的值为none并且匹配失败,因为"99%"是被禁止的。但是使用我的正则表达式,我得到了"x = 100%"和"y = 9"。
如何修复我的正则表达式?