我想從我的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
刪除外部圓括號。 –
@WiktorStribiżew對不起,我吸食正則表達式。非常感謝 – Ludisposed
在這裏使用正則表達式有什麼特別的理由嗎?看起來更像是一個'split()'的工作。 –