Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tcl:删除磅符号注释行_Tcl - Fatal编程技术网

Tcl:删除磅符号注释行

Tcl:删除磅符号注释行,tcl,Tcl,为什么我不能删除英镑符号注释行 #!/usr/bin/tclsh set lines [list file1.bmp { # file2.bmp} file3.bmp ] # Now we apply the substitution to get a subst-string that # will perform the computational parts of the conversion. set out [regsub -all -line {^\s*#.*$} $lines

为什么我不能删除英镑符号注释行

#!/usr/bin/tclsh

set lines [list file1.bmp {  # file2.bmp} file3.bmp ]

# Now we apply the substitution to get a subst-string that
# will perform the computational parts of the conversion.
set out [regsub -all -line {^\s*#.*$} $lines {}]

puts $out
输出:

file1.bmp {  # file2.bmp} file3.bmp
file1.bmp file3.bmp
file1.bmp {
-更新-

预期产出:

file1.bmp {} file3.bmp
{}
表示空字符串

事实上,这是我的第一步。我的最终目标是消除所有注释行和所有空行。上述问题仅将所有注释行更改为空行。例如,如果输入为:

set lines [list file1.bmp {  # file2.bmp} {} file3.bmp ]
我希望我的最终结果是

file1.bmp file3.bmp
注意:Stackoverflow错误地将英镑(#)符号前后的所有内容调暗,认为这些是注释。但在TCL语法中,它不应该是注释

@坦西拜:
我还想删除空行,因此匹配“#”之前的任意数量的空格。(因为在删除所有包含的“#”之后,它是一个空行)。事实上,在我的数据中,注释本身总是显示为一整行。但是“#”符号可能不会出现在第一个字符=>空格可以引导注释行。

编辑后回答:

#/usr/bin/tclsh
设置行[list file1.bmp{#file2.bmp}file3.bmp#test]
把$line
#现在我们应用替换来获得一个subst字符串
#将执行转换的计算部分。
设置[lsearch-regexp-all-inline-not$lines{^\s*(#.*)?$}]
拿出美元
输出:

file1.bmp {  # file2.bmp} file3.bmp
file1.bmp file3.bmp
file1.bmp {
您正在处理一个
列表
列表
的表示是一个简单的文本,因此您可以
regsub
它,但它是一行。 如果要检查此列表中的元素,必须使用与列表相关的命令

在这里,
lsearch
将执行您所做的操作,检查每个项目是否与正则表达式匹配,
-not
告诉返回与
-all-inline不匹配的元素


旧答案:

原因:因为您的正则表达式与任何一磅匹配,前面只有0或无限数量的空格。因此,它将只匹配注释行,而不匹配内联注释

看看如何测试正则表达式

一个有效的正则表达式是:

#!/usr/bin/tclsh

set lines [list file1.bmp {  # file2.bmp} file3.bmp ]

# Now we apply the substitution to get a subst-string that
# will perform the computational parts of the conversion.
set out [regsub -all -line {^(.*?)#.*$} $lines {\1}]

puts $out
对于正则表达式(完整详细信息):

  • ^
    匹配行首
  • (.*)#
    在#之前匹配并捕获尽可能有限的字符数(非贪婪运算符?用于限制匹配)
  • *$
    匹配任何数量的字符,直到行尾
我们将替换为
\1
,这是第一个捕获组(也是本例中唯一的一个)

输出:

file1.bmp {  # file2.bmp} file3.bmp
file1.bmp file3.bmp
file1.bmp {

这也将删除整行注释,但如果在磅符号前有空格或制表符,则可能会留下空格或制表符,因此留下空行。

您希望得到什么输出?@RobinHsu更新了答案谢谢。这很有效。但是我对它做了一点调整:
设置[lsearch-regexp-all-inline-not$lines{^\s*(#.*)?$}]
以同时删除空行。@RobinHsu是的,我没有考虑它,我更新了答案。谢谢你的邀请update@robin我之所以回滚巡演编辑,是因为1)它无法消除空行,2)编辑摘要没有理由放在答案中