[英]Python regular expression for PHP array parsing
我有一個函數,可以從文件中解析PHP數組聲明。 然后,該函數返回一個字典,其中的鍵是PHP數組的鍵,而python中的值是來自PHP數組的值。
$lang['identifier_a'] = 'Welcome message';
$lang['identifier_b'] = 'Welcome message.
You can do things a,b, and c here.
Please be patient.';
$lang['identifier_c'] = 'Welcome message2.
You can do things a,b, and c here.
Please be patient.';
$lang['identifier_d'] = 'Long General Terms and Conditions with more text';
$lang['identifier_e'] = 'General Terms and Conditions';
$lang['identifier_f'] = 'Text e';
def fetch_lang_keys(filename):
from re import search;
import mmap;
''' fetches all the language keys for filename '''
with open(filename) as fi:
lines = fi.readlines();
data = {};
for line in lines:
obj = search("\$lang\[[\'|\"](.{1,})[\'|\"]\] = [\'|\"](.{1,})[\'|\"];", line);
# re.match(r'''\$lang\[[\'|\"](.{1,})[\'|\"]\] = [\'|\"](.{1,})[\'|\"];''', re.MULTILINE | re.VERBOSE);
if obj:
data[obj.group(1)] = obj.group(2);
return data;
此函數應返回一個如下所示的字典:
data['identifier_a'] = 'Welcome message'
data['identifier_b'] = 'Welcome message.
You can do things a,b, and c here.
Please be patient.';
// and so on
該函數中使用的regexp適用於除identifier_b和identifier_c之外的所有內容,因為正則表達式與空白行和/或不以;結尾的行不匹配。 帶;的通配符 最后還是起作用了,因為匹配太多了。
您對如何解決這個問題有任何想法嗎? 我調查了前瞻性斷言,但未能正確使用它們。 謝謝。
好吧,為什么我的答案不是您的正則表達式問題的解決方案,但是:為什么您不希望使用“真正的PHP解析器”而不是自制的正則表達式呢? 它可能更加可靠,甚至可能更快,並且肯定是一種更具維護性的解決方案。
快速谷歌搜索給了我: https : //github.com/ramen/phply 。 但我也發現了這一點: 從Python腳本解析PHP文件變量 。 希望能有所幫助。
它不起作用,因為點與換行符不匹配。 您必須使用re.DOTALL
修飾符( re.DOTALL
)而不是多行修飾符。 例:
obj = re.search(r'\$lang\[[\'"](.+?)[\'"]\] = [\'"](.+?)[\'"];', line, re.DOTALL);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.