我有一个包含以下数据的文件svn_log.txt:
:SUMMARY: This module is test created
:TIME: the current time is not listed我正在使用tcl和regex从这个文件中提取摘要。
set svn_logs svn_logs.txt
set fp [open $svn_logs r]
set lines [split [read -nonewline $fp] "\n"]
close $fp
foreach line $lines {
if {[regexp -nocase {^\s*(:SUMMARY)\s*:\s*(.*)$} $line match tag value]} {
set [string tolower $tag] $value
}
}
puts $value它可以正常工作,直到摘要只有一行。但是,在某些情况下,摘要有几个要点:
:SUMMARY: Following changes needs to be added
1. this one
2. this one too
:TIME:在这种情况下,它只提取第一行内容。我很难尝试修改上面的regex命令,以获取:SUMMARY和:TIME之间的任何内容。新来的。有人能提供任何投入吗?
文件->的原始内容
------------------------------------------------------------------------
r743 | aaddh | 2014-04-01 12:33:42 -0500 (Tue, 01 Apr 2014) | 8 lines
:SUMMARY: Modified file to add following changes:
1.Loop to avoid .
2.Change directory
3.The batch file
:TIME: Invalid
:Test:
:Comments:发布于 2014-04-02 15:32:34
如果您真的想使用regex,则必须使用不同的方法。您必须一次性读取整个文件,并在其上使用regex:
set svn_logs svn_logs.txt
set fp [open $svn_logs r]
set lines [read -nonewline $fp]
close $fp
regexp -nocase -lineanchor -- {^\s*(:SUMMARY)\s*:\s*(.*?):TIME:$} $lines match tag value
puts $value作为投入:
:SUMMARY: Following changes needs to be added
1. this one
2. this one too
:TIME:你得到:
Following changes needs to be added
1. this one
2. this one too码页演示
-lineanchor标志使^匹配行的所有开头,$匹配所有行尾。--只是确保没有额外的标志。
注意:在捕获组的末尾有一个挥之不去的换行符,如果需要,您可以修剪它。
发布于 2014-04-02 17:12:57
regexp解决方案非常紧凑。如果您正在读取文件的行,您可以:
set fh [open file r]
set insumm false
while {[gets $fh line] != -1} {
switch -regex -- $line {
{^:SUMMARY:} {set insumm true; set summary [string range $line 10 end]}
{^:\w+:} break
default {if {$insumm} {append summary \n $line}}
}
}
close $fh发布于 2014-04-02 15:26:42
您可以尝试这样的方法:[^:SUMMARY:](.*)[^:TIME:]
https://stackoverflow.com/questions/22816038
复制相似问题