[英]Python: Extract substring that matches regular expression in a string
I am trying to fetch some database details from wp-config.php file. 我正在尝试从wp-config.php文件中获取一些数据库详细信息。 Unfortunately I stick on the part where I have the line that contains the DB_NAME and I could not extract the database_name only from the following: 不幸的是,我坚持使用包含DB_NAME的行,并且无法仅从以下内容中提取database_name :
define('DB_NAME', 'database_name');
Help is much appreciated! 非常感谢帮助!
Iterate over each line and then apply re.search
like below. 遍历每一行,然后应用re.search
如下所示。
>>> x = "define('DB_NAME', 'database_name');"
>>> re.search(r"define\('DB_NAME',\s*'([^']*)'\);", x).group(1)
'database_name'
or 要么
for line in f:
if 'DB_NAME' in line:
print line.split("'")[3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.