一
1.1 字典项目匹配
输入字符串
text = """
{
'layer':
{
'unit1': 'hello'
'unit2': 'world',
},
'block':
{
'unit1': 'hehe'
}
}
"""
正则表达式
import re
findItem = re.findall(f"'layer'.*?('unit1'.*?'.*?')\n",text,re.S)[0]
print(findItem)
输出
解释:
(1)多行字符串使用:在python使用三引号来输入多行字符串""" """
(2)如果想在字符串中,使用单引号,则外面使用双引号,总之就是里外要不同
(3)一般使用findall去找到匹配的字符串,返回的是一个列表
(4)?表示贪婪匹配,一般用在.*的后面,结束标志的前面