2016-04-11 7 views
-1

画像リンクhere。 私は、次のルータの出力があります。シンプルなPython正規表現で単語を区切る

['bandwidth' , 'beacon' , 'cdp' , 'channel-group' , 'delay' , 'description'] 

私は

m = re.compile('(\w+\s\w+)') 
n = m.findall(buffer) 

をしようと、この出力を取得しています:

['config t', 'Enter configuration', 'one per', 'End with', 'profile demo_ethernet', 'Set bandwidth', 'informational parameter', 'enable the', 'beacon for', 'an interface', 'Configure CDP', 'interface parameters', 'Configure port', 'channel parameters', 'Specify interface', 'throughput delay', 'Enter port', 'profile description', 'of maximum', '80 characters'] 
私はこれが欲しい、上記の出力から

config t 
Enter configuration commands, one per line. End with CNTL/Z. 
n7k(config)# port-profile demo_ethernet 
n7k(config-port-prof)# ? 
    bandwidth  Set bandwidth informational parameter 
    beacon   Disable/enable the beacon for an interface 
    cdp    Configure CDP interface parameters 
    channel-group Configure port channel parameters 
    delay   Specify interface throughput delay 
    description  Enter port-profile description of maximum 80 characters 

+0

マッチングのルールは何ですか? –

+0

左側のキーが必要です。最初の4行の後に。 – ursandy

+0

データを正しくフォーマットしてください。 1つの長い文字列として表示されます。 –

答えて

0

このreg exは(起動中のスペースを持っているあなたが合うように好きなラインと仮定して)動作するはず

^\s+([\w-]+)\s+.+$ 

Regex Demo

Pythonコード(簡単にするために、私は単一の文字列内のすべての入力をとっている)

p = re.compile(r'^\s+([\w-]+)\s+.+$', re.MULTILINE) 
test_str = "config t\nEnter configuration commands, one per line. End with CNTL/Z.\nn7k(config)# port-profile demo_ethernet\nn7k(config-port-prof)# ?\n bandwidth  Set bandwidth informational parameter\n beacon   Disable/enable the beacon for an interface\n cdp    Configure CDP interface parameters\n channel-group Configure port channel parameters\n delay   Specify interface throughput delay\n description  Enter port-profile description of maximum 80 characters" 

print(re.findall(p, test_str)) 

Ideone Demo

+0

@ursandyこの 'regex'はあなたの要件を満たしていますか? – rock321987

+0

はい。ありがとうございました。 @ rock321987 – ursandy

+0

が受け入れられました。 しかし、なぜ私の質問は-1と評価されていますか? – ursandy

関連する問題