我正在分析/etc/mtab
,希望捕获第二个字段和第四个字段中的subvol
或subvolid
设置(如果有的话)。但是,我在制定正确的正则表达式时遇到了一些困难。请看:
import re
def test(regex):
def helper(string):
m = re.match(regex, string)
if m is None: print("no matches")
else: print(m.groups())
helper("/dev/sdb2 /mnt/btrfs btrfs rw,noatime 0 0")
helper("/dev/sdb2 /tmp btrfs rw,noatime,subvol=os-aux/kubuntu-lts/tmp 0 0")
helper("/dev/sdb2 /tmp btrfs noatime,subvol=os-aux/kubuntu-lts/tmp,rw 0 0")
helper("/dev/sdb2 /tmp btrfs subvol=os-aux/kubuntu-lts/tmp,rw,noatime 0 0")
当然,预期的结果是:
('/mnt/btrfs', None)
('/tmp', 'subvol=os-aux/kubuntu-lts/tmp')
('/tmp', 'subvol=os-aux/kubuntu-lts/tmp')
('/tmp', 'subvol=os-aux/kubuntu-lts/tmp')
现在我的实验和结果显示:
>>> test("\S+ (\S+) \S+ \S+(subvol(?:id)?=[^ ,]+)?")
('/mnt/btrfs', None)
('/tmp', None)
('/tmp', None)
('/tmp', None)
>>> test("\S+ (\S+) \S+ \S+?(subvol(?:id)?=[^ ,]+)?")
('/mnt/btrfs', None)
('/tmp', None)
('/tmp', None)
('/tmp', None)
>>> test("\S+ (\S+) \S+ \S+(subvol(?:id)?=[^ ,]+)")
no matches
('/tmp', 'subvol=os-aux/kubuntu-lts/tmp')
('/tmp', 'subvol=os-aux/kubuntu-lts/tmp')
no matches
我做错什么了?如何制定正则表达式来实现我的目标?你知道吗
谢谢。你知道吗
这个适合我
此外,这是一个超级有用的网站正则表达式 https://www.debuggex.com/
编辑:
这个也适用于没有子卷的:
相关问题 更多 >
编程相关推荐