I'm attempting to parse /etc/mtab
but exclude /boot
. I thought perhaps non-capturing groups would be the way to go, but it doesn't work as I expected. This is the regex I constructed:
我试图解析/etc/mtab但排除/引导。我想也许不抓人的团体会是我的出路,但它并没有像我预期的那样发挥作用。这是我构造的regex:
proc = subprocess.Popen(["ssh", server, "cat", mtab],stdout = subprocess.PIPE)
for line in proc.stdout:
fsMatch = re.search(r'([\w/:]+) (/([\w/:-]+)|(?:boot)) (nfs|ext3)', line)
if fsMatch:
print fsMatch.group(1,2,4)
Output:
输出:
('/dev/sda1', '/boot', 'ext3')
('/dev/mapper/foo1', '/export/foo1', 'ext3')
('/dev/mapper/foo2', '/export/foo2', 'ext3')
('/dev/mapper/foo3', '/export/foo3', 'ext3')
('/dev/mapper/foo4', '/export/foo4', 'ext3')
('/dev/mapper/foo5', '/export/foo5', 'ext3')
('servernfs:/install', '/mnt', 'nfs')
I'm pretty confident the |
is wrong (and obviously more is wrong) but have hit a roadblock.
我很有信心|是错误的(显然更多是错误的),但是遇到了障碍。
I'm looking for all matches for /[\w/:-]+
but exclude matches to /boot
我正在寻找/[\w/:-]+的所有匹配项,但排除对/boot的匹配项
Suggestions?
建议吗?
2 个解决方案
#1
2
You need to use a negative lookbehind or negative lookahead, described here with a hint below:
你需要使用一个消极的前视或消极的前视,这里有一个提示:
r'^(?!/boot).*$'
If you need to capture that 'servernfs:' one and not 'servernfs:/boot', you'll need to sprinkle in a little '|' and '([a-z]+:)' somewhere at the top (after the '^')
如果您需要捕获“servernfs:“,而不是“servernfs:/ boot”,你需要洒在一个小“|”和“([a - z]+:)”顶部的地方(在“^”)
#2
1
Just exclude the line:
排除线:
for line in proc.stdout:
if 'boot' not in line:
# the rest
However, since mtab
is delimited on space, you can just use split
:
但是,由于mtab在空间上是分隔的,所以可以使用split:
>>> with open('foo.txt') as f:
... lines = [line.split(' ') for line in f if 'boot' not in line]
...
#1
2
You need to use a negative lookbehind or negative lookahead, described here with a hint below:
你需要使用一个消极的前视或消极的前视,这里有一个提示:
r'^(?!/boot).*$'
If you need to capture that 'servernfs:' one and not 'servernfs:/boot', you'll need to sprinkle in a little '|' and '([a-z]+:)' somewhere at the top (after the '^')
如果您需要捕获“servernfs:“,而不是“servernfs:/ boot”,你需要洒在一个小“|”和“([a - z]+:)”顶部的地方(在“^”)
#2
1
Just exclude the line:
排除线:
for line in proc.stdout:
if 'boot' not in line:
# the rest
However, since mtab
is delimited on space, you can just use split
:
但是,由于mtab在空间上是分隔的,所以可以使用split:
>>> with open('foo.txt') as f:
... lines = [line.split(' ') for line in f if 'boot' not in line]
...