2016-08-26 11 views
0

このパターンの左右の部分を区切るには正規表現が必要です。 。 。 。 。 : Tclの正規表現で行を解析する

Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : alumnus.co.in 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 

と2つの変数に格納します。 私はこの正規表現を書いています

regexp {([[a-z]*[0-9]*.*[0-9]*[a-z]*]*" "):([[a-z]*[0-9]*.*[0-9]*[a-z]*]*)} 6*rag5hu. . :4ku5-1a543m match a b 

しかし、動作しません。 助けていただければ幸いです。

答えて

0

私はこれを行うだろう:

set text {Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : alumnus.co.in 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes} 

foreach line [split $text \n] { 
    if {[regexp {^(.+?)(?: \.)+ : (.+)$} $line -> name value]} { 
     puts "$name => $value" 
    } 
} 

出力

Media State => Media disconnected 
    Connection-specific DNS Suffix => alumnus.co.in 
    Description => Microsoft ISATAP Adapter 
    Physical Address. => 00-00-00-00-00-00-00-E0 
    DHCP Enabled. => No 
    Autoconfiguration Enabled => Yes 

これは、非欲張り数量詞(+?)を使用し、それは正規表現非欲張り内のすべての数量詞作ります。キャプチャするビットに必要なすべてのテキストが含まれるように、アンカーが必要です。

+0

無関係のビットを取り除くために、おそらく 'string trim $ name"を実行する価値があります。 –

0

借入textの定義:

package require textutil 

foreach line [split $text \n] { 
    lassign [::textutil::splitx [string trim $line] {\s*(?:\.)+:\s*}] a b 
    puts "a: $a\nb: $b" 
} 

出力

a: Media State 
b: Media disconnected 
a: Connection-specific DNS Suffix 
b: alumnus.co.in 
a: Description 
b: Microsoft ISATAP Adapter 
a: Physical Address 
b: 00-00-00-00-00-00-00-E0 
a: DHCP Enabled 
b: No 
a: Autoconfiguration Enabled 
b: Yes 

ドキュメント与える: foreachlassignpackageputssplit、 に、 textutil (package)

関連する問題