2017-10-12 124 views
3

我想從我的UNIX機器上的\etc\services文件捕獲一些信息,但我捕獲了錯誤的值,同時也使得它過於複雜。正則表達式來捕獲'/ etc/services'

我現在有

with open('/etc/services') as ports_file: 
    lines = ports_file.readlines() 
    for line in lines: 
     print re.findall('((\w*\-*\w+)+\W+(\d+)\/(tcp|udp))', line) 

但它產生不正確的值這樣的內容:

[('dircproxy\t57000/tcp', 'dircproxy', '57000', 'tcp')] 
[('tfido\t\t60177/tcp', 'tfido', '60177', 'tcp')] 
[('fido\t\t60179/tcp', 'fido', '60179', 'tcp')] 

我希望它是這樣的:

[('dircproxy', '57000', 'tcp')] 
[('tfido', '60177', 'tcp')] 
[('fido', '60179', 'tcp')] 

我覺得這我的正則表達式需要(\w*\-*\w+)+,因爲有些是def像這樣的this-should-capture

+1

刪除外部圓括號。 –

+0

@WiktorStribiżew對不起,我吸食正則表達式。非常感謝 – Ludisposed

+1

在這裏使用正則表達式有什麼特別的理由嗎?看起來更像是一個'split()'的工作。 –

回答

1

我建議從不同的角度來看這個:不匹配字段值,匹配它們之間的分隔符。

print re.split(r'[\s/]+', line.split('#', 1)[0])[:3] 

第一line.split('#', 1)[0]刪除意見(文件中的第一#後的任何東西)。

0

它個人不會在這裏使用正則表達式。查看下面的解決方案,並嘗試查看它是否符合您的需求(還請注意,您可以直接遍歷文件對象):

services = [] 
with open('/etc/services') as serv: 
    for line in serv: 
     l = line.split() 
     if len(l) < 2: 
      continue 
     if '/tcp' in l[1] or '/udp' in l[1]: 
      port, protocol = l[1].split('/') 
      services.append((l[0], port, protocol))