2016-08-27 20 views
-2

我有以下線在我的文件我如何得到重複文本後話在一條線上

你的圖像不列入匹配圖像我腦子裏想的

我需要查找的單詞在這條線的圖像,並打印下一字隨後它 即我需要以下O/p:

word_1 =的
word_2 = I

我有regexp命令找到這個詞圖片但我怎麼才能找到成功的單詞,而無需使用lsearch cmd?

回答

0

您必須使用返回匹配數組的參數-inline

所以,你可以在這裏一個例子:

set text "The image of yours doesnot match the image I had in mind" 

set i 0 
set word_1 "" 
set word_2 "" 
set words [list ] 

foreach {img _get} [regexp -all -inline -- {image ([a-zA-Z]+)} $text] { 
    # print out the word after "image" 
    puts $_get 

    # this if you want to save in a list 
    lappend words $_get 

    # here you can save on separate variables 
    if {$i == 0} { 
    set word_1 $_get 
    } else { 
    set word_2 $_get 
    } 
    incr i 
} 

使用列表是一個更加靈活的方法,但如果你已經認識的單詞的確切數目,將匹配的句子,比單變量應該適應好。

+0

我可以將其設置爲可變像下面* text1 = text1 = I * 我需要將每個單詞設置爲一個變量並在腳本中使用該變量 – johnny

0

你可以這樣說:

set txt {The image of yours doesnot match the image I had in mind} 

set words [split $txt] 
for {set i 0} {$i < [llength $words]} {incr i} { 
    if {[lindex $words $i] eq "image"} { 
     puts [lindex $words [incr i]] 
    } 
} 

該解決方案看起來在序列中的每個字。如果它等於「圖像」,它會打印下列單詞,然後繼續處理列表中的下一個單詞。

編輯

爲了節省每發現字的變量,並立即使用,以取代puts [lindex $words [incr i]]

 set found [lindex $words [incr i]] 
     # do something with $found 

爲了節省每找到單詞的列表,並處理所有後語找到他們全部,替換相同的行:

 lappend found [lindex $words [incr i]] 

這是一個好主意,設置found搜索單詞之前的空列表。

文檔: < (operator)eq (operator)forifincrlappendlindexllengthputssetsplit

+0

我可以將它設置爲像下面這樣的變量* text1 = of * * text1 = I *我需要將每個單詞設置爲一個變量並使用該變量在我的腳本 – johnny

相關問題