假设我有一个字符串,例如
我尝试了这段代码:
test-123
。
我想测试它是否匹配类似于test-<数字>
的模式,其中<数字>
表示一个或多个数字符号。我尝试了这段代码:
import re
correct_string = 'test-251'
wrong_string = 'test-123x'
regex = re.compile(r'test-\d+')
if regex.match(correct_string):
print 'Matching correct string.'
if regex.match(wrong_string):
print 'Matching wrong_string.'
如何使只有correct_string
与之匹配,而wrong_string
不匹配?我尝试使用.search
而不是.match
但没有帮助。