2015-10-26 111 views

回答

2

迭代每行,然後應用re.search,如下所示。

>>> x = "define('DB_NAME', 'database_name');" 
>>> re.search(r"define\('DB_NAME',\s*'([^']*)'\);", x).group(1) 
'database_name' 

for line in f: 
    if 'DB_NAME' in line: 
     print line.split("'")[3]