【发布时间】:2012-03-04 03:23:51
【问题描述】:
有人知道用于 grub2 的 grub.cfg 文件的 python 解析器吗?
我正在尝试通过设备分区(根)获取“菜单项”,例如
hd0,msdos1: ['Ubuntu, with Linux 3.0.0-15-generic',
'Ubuntu, with Linux 3.0.0-15-generic (recovery mode)',
'Ubuntu, with Linux 3.0.0-14-generic']
hd2,msdos1: ["Windows 7 (on /dev/sdc1)"]
等等
解决方案:
re.findall("menuentry ['\"](.*?)['\"].*?set root='(.*?)'", x, re.S)
[('Ubuntu, with Linux 3.0.0-15-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-15-generic (恢复模式)', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-14-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-14-generic (恢复模式)', '(hd0,msdos1)'), ('Ubuntu, with Linux 3.0.0-13-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-13-generic (恢复模式)', '(hd0,msdos1)'), ('Ubuntu,使用 Linux 3.0.0-12-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-12-generic (恢复模式)', '(hd0,msdos1)'), ('内存测试 (memtest86+)', '(hd0,msdos1)'), ('内存测试 (memtest86+, 串行控制台 115200)', '(hd0,msdos1)'), ('Windows 7 (在 /dev/sdc1)', '(hd2,msdos1)')]
【问题讨论】:
-
哦,你们希望一切都为您完成...只要制作那个解析器,您就会有很多乐趣!
-
不,我只是问有没有。无论如何,找到了正则表达式和 re.S (DOTALL) 的替代方法。