每当任务(对于patternStart.finditer(TestString) 中的匹配)未找到匹配项时,我都会尝试打印(未找到匹配项)。我一直坚持这个没有任何成功。任何意见,将不胜感激。
字符串“jeke”故意不包含数字,因此找不到匹配项。
TestString = 'jeke'
patternStart = re.compile(r'\d')
cnt = 0 # Initialize the counter
wanted1 = [1] # Defines the 1-based IDs of the matches you want to display
wanted2 = [2] # Defines the 1-based IDs of the matches you want to display
for match in patternStart.finditer(TestString):
cnt += 1
if cnt in wanted1:
out = match.group()
else:
print('match was not found')
我已经尝试过了。
if match.group() is None:
print('match was not found')
if out is None:
print('match was not found')
凤凰求蛊
相关分类