在tcl中的文件中捕获多个空行 [英] catch multiple empty lines in file in tcl

查看:42
本文介绍了在tcl中的文件中捕获多个空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件中有 4 个空格,在 wr_fp 中设置.我想在代码中捕获四个空格.但下面的代码不起作用.

while {[gets $wr_fp line3] >= 0} {if {[regexp "\n\s+\n\s+\n\s+\n" $line3]} { puts "found 4 empty lines"}}

解决方案

tl;dr: 不要把 RE 放在 "quotes",把它们放在{大括号}中.

问题在于您将 RE 放在引号中,因此实际上是这样的:

<前>s+s+s+

由于 Tcl 的一般替换规则\n 成为换行符,\s 成为简单的 s.将 RE 放在大括号中会抑制这种(在本例中是不需要的)行为.

There are 4 empty space in my file,set in wr_fp.I want to catch four empty space in code. But below code is not working.

while {[gets $wr_fp line3] >= 0} {
if {[regexp "\n\s+\n\s+\n\s+\n" $line3]} { puts "found 4 empty lines"}
}

解决方案

tl;dr: Don't put REs in "quotes", put them in {braces}.

The problem is that you've put your RE in quotes, so that it is actually this:


s+
s+
s+

Because of Tcl's general substitution rules, \n becomes a newline and \s becomes a simple s. Putting the RE in braces inhibits this (unwanted in this case) behaviour.

这篇关于在tcl中的文件中捕获多个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆