Reputation: 471
I have the following string
"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 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
Upvotes: 0
Views: 21563
Reputation: 16436
There can be many ways. I have tried with 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"
which will produce the following
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.
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.
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
.
Notice the use of -all
flag at the beginning which is responsible for matching all the occurrence of this pattern.
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
Reference : Non-capturing subpatterns
Upvotes: 4
Reputation: 137787
You seek to remove all "
characters from the string that have at least one character on each side of them. That leads to this regular expression substitution:
set transformedString [regsub -all {(.)[""]+(.)} $inputString {\1\2}]
The "
is doubled up and in [
brackets]
just to make the highlighting here work. You could use {(.)"+(.)}
instead.
Upvotes: 1
Reputation: 247210
You could remove all quotes and re-add the outer ones. One way:
set new [format {"%s"} [string map {{"} {}} $str]]
Upvotes: 2