import re # str = input() str = 'read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]' str = str.split(' ') pat = str[0] s = str[1] # update : []里面默认就是或,所以|其实是被当作字面量了,原来的写法是错的,和理论的(a|b)搞混了 # num = '(0[x|X][a-f|A-F|0-9]*)' num = '(0[xX][a-fA-F0-9]*)' pattern = pat + '\[addr=' + num + ',mask=' + num + ',val=' + num + '\]' res = re.findall(pattern, s) for i in res: print(i[0], i[1], i[2], end = '\r\n') if len(res) == 0: print('FAIL', end = '\r\n')