如何在文件内的双引号内提取单词?例如
variable "xxx"
Reading a text file into Tcl is just this:
set fd [open $filename]
set data [read $fd] ;# Now $data is the entire contents of the file
close $fd
To get the first quoted string (under some assumptions, notably a lack backslashed double quote characters inside the double quotes), use this:
if {[regexp {"([^""]*)"} $data -> substring]} {
# We found one, it's now in $substring
}
(Doubling up the quote in the brackets is totally unnecessary — only one is needed — but it does mean that the highlighter does the right thing here.)
The simplest method of finding all the quoted strings is this:
foreach {- substring} [regexp -inline -all {"([^""]*)"} $data] {
# One of the substrings is $substring at this point
}
Notice that I'm using the same regular expression in each case. Indeed, it's actually good practice to factor such REs (especially if repeatedly used) into a variable of their own so that you can “name” them.
Combining all that stuff above:
set FindQuoted {"([^""]*)"}
set fd [open $filename]
foreach {- substring} [regexp -inline -all $FindQuoted [read $fd]] {
puts "I have found $substring for you"
}
close $fd
如果您只是在寻找正则表达式,那么您可以使用 TCL 的捕获组。例如:
set string {variable "xxx"}
regexp {"(.*)"} $string match group1
puts $group1
这将返回xxx
,丢弃引号。
如果您想匹配文件中的数据而无需直接将文件读入 TCL,您也可以这样做。例如:
set match [exec sed {s/^variable "\(...\)"/\1/} /tmp/foo]
这将调用 sed 来查找您想要的匹配部分,并将它们分配给 TCL 变量以进行进一步处理。在此示例中,匹配变量设置xxx
为如上,但操作的是外部文件而不是存储的字符串。
When you just want to find with grep
all words in quotes in a file and do something with the words, you do something like this (in a shell):
grep -o '"[^"]*"' | while read word
do
# do something with $word
echo extracted: $word
done