Tcl 如何在一行重复文本后获取单词

Tcl 如何在一行重复文本后获取单词,tcl,Tcl,我的文件中有下面一行 你的形象与我心目中的形象不符 我需要找到这一行中的单词图像,然后打印它后面的下一个单词 i、 e我需要以下o/p: 单词_1=of 单词_2=I 我有regexp命令来查找单词图像,但是如何在不使用lsearch cmd的情况下查找后面的单词???您必须使用参数-inline来返回匹配数组 你可以举个例子: set text "The image of yours doesnot match the image I had in mind" set i 0 set wor

我的文件中有下面一行

你的形象与我心目中的形象不符

我需要找到这一行中的单词图像,然后打印它后面的下一个单词 i、 e我需要以下o/p:

单词_1=of 单词_2=I


我有regexp命令来查找单词图像,但是如何在不使用lsearch cmd的情况下查找后面的单词???

您必须使用参数-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
}

使用列表是一种更灵活的方法,但是如果您已经知道与句子匹配的确切单词数,那么单个变量应该很合适。

您可以这样做:

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]]
    }
}
要将找到的每个单词保存在列表中,并在找到所有单词后处理所有单词,请将同一行替换为:

        lappend found [lindex $words [incr i]]
在搜索单词之前,最好将find设置为空列表

文件: , , , , , , , , ,

我是否可以将其设置为下面的变量text1=of text1=我需要将每个单词设置为变量并在脚本中使用该变量我是否可以将其设置为下面的变量text1=of text1=我需要将每个单词设置为变量并在脚本中使用该变量
        lappend found [lindex $words [incr i]]