如何使用TCL删除字符串中的字符 [英] How to delete a character in a string using TCL

查看:96
本文介绍了如何使用TCL删除字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串

TCL 被称为工具命令语言",TCL 被称为工具命令语言"命令语言",TCL被称为工具命令语言""

"TCL is known as "tool command language", TCL is known as "tool command language", TCL is known as "tool command language""

从上面的输入我想要一个像下面这样的输出

from the above input I want a output like below

"TCL被称为工具命令语言,TCL被称为工具命令语言,TCL 被称为工具命令语言"

"TCL is known as tool command language, TCL is known as tool command language, TCL is known as tool command language"

只应在输出中显示第一个和最后一个双引号,其他的都应删除,有人可以告诉我实现此目的的不同方法

i.e. only first and last double quotes should be displayed on output, and all other should be deleted, Could someone let me know the different methods to accomplish this

推荐答案

有很多方法.我试过 regsub

set str {"TCL is known as "tool command language", TCL is known as "tool command language", TCL is known as "tool command language""}
puts "Input : $str"
regsub -all {(.)"} $str {\1} output
puts "Output : $output"

将产生以下内容

Input : "TCL is known as "tool command language", TCL is known as "tool command language", TCL is known as "tool command language""
Output : "TCL is known as tool command language, TCL is known as tool command language, TCL is known as tool command language"

我使用的模式是 (.)".在正则表达式中,原子 . 将匹配任何单个字符.(将在后面讨论括号的用法底部).然后是单引号.所以,基本上,这将匹配任何单个字符并在其旁边有一个单引号,如下所示.

The pattern I have used is (.)". In regular expressions, the atom . will match any single character. (Will talk about the parenthesis usage at the bottom). Then a single quote. So, basically, this will match any single char and having a single quote next to it as shown below.

如您所见,我们共有 6 场比赛.让我们取第二个匹配项,即 e".我们的主要目的是删除引号.但是,我们匹配了 2 个字符.这就是我们用括号将其分组的原因.

As you can see, we have a total of 6 matches. Let us take the 2nd match which is e". Our main intention is to remove the quotes. But, we have matched 2 characters. This is the reason why we have grouped it with parenthesis.

使用Tcl,我们可以通过\1 访问第一个子组,通过\2 访问第二个子组,依此类推.最后,我们用一个字符代替这两个字符,它只是引号以外的第一个字母.即 e" 替换为字符 e.

With Tcl, we can access 1st subgroup with the help of \1 and 2nd subgroup with \2 and so on. Finally, we are substituting the 2 characters with one character which is nothing but the first letter other than quote. i.e. e" is substituted with character e.

注意在开头使用了 -all 标志,它负责匹配此模式的所有出现.

Notice the use of -all flag at the beginning which is responsible for matching all the occurrence of this pattern.

注意: \1 应该与我提到的 {\1} 之类的大括号一起使用.如果你想不带大括号访问它,你必须使用 \\1

Note : \1 should be used with braces like {\1} as I have mentioned. In case, if you want to access it without braces, you have to use \\1

参考:非捕获子模式

这篇关于如何使用TCL删除字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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