I am not able to figure the StringPattern to use to remove markers in string.
This is the input.
lst = {{1, 2, "this is a test", 4}, {Pi, 20, xy, 10}};
buf = ToString@TeXForm@lst
which gives
\left(
\begin{array}{cccc}
1 & 2 & \text{this is a test} & 4 \\
\pi & 20 & \text{xy} & 10 \\
\end{array}
\right)
I need to remove all the places where this pattern shows up \text{.....}
and replace it with just what is inside ....
. i.e. strip out the \text{
and the closing }
on the other side. For each such instance in the input.
So the above should become
\left(
\begin{array}{cccc}
1 & 2 & this is a test & 4 \\
\pi & 20 & xy & 10 \\
\end{array}
\right)
I tried many things. Tried also using RegularExpression
.
One attempt:
StringReplace[buf, "\\text{" ~~ x___ ~~ "}" .. :> x]
But this has a problem. It does not stop at the first closing }
, but goes all the way to the ending }
in the string, ending up with
\left(
\begin{array}{cccc}
1 & 2 & this is a test} & 4 \\
\pi & 20 & \text{xy} & 10 \\
\end{array
\right)
Notice, it went all the way to the end, and removed the }
after {array
.
I did not know how to tell it to stop at the first }
it sees after it sees \text{
. And that is what I am struggling with. I know I wrote x__
but I needed to do this, so I can pick out the x
.
Any idea how to do this? Either using StringPattern or ReqgularExpression will work.