原始字符串我要读的线中,我寻找图案像蟒:创建的变量
width:40
height :50
left : 60
right: 70
以下发现在上面的代码中所需的图案
line = "width:40"
match = re.search(r'width\s*:\s*\d+', line)
我已经硬编码为width
正则表达式模式我已存储的所有四个变量在阵列key_word = ['width', 'height', 'left', 'right']
我要搜索的模式,所有这些变量像
for key in key_word:
match = re.search(key, line)
问题是我怎么能做出这种key
一个原始的字符串,像
r'width\s*:\s*\d+'
r'height\s*:\s*\d+'
r'left\s*:\s*\d+'
r'right\s*:\s*\d+'