2016-08-27 58 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

相关问题