[texhax] Low-level TeX question: string substitution macro
Toby Cubitt
tsc25 at cantab.net
Tue May 29 17:31:32 CEST 2007
Michael Doob wrote:
> On Tuesday 29 May 2007 8:38 am, Toby Cubitt wrote:
>> I'm trying to write an internal macro that does string substitution, in
>> order to escape certain characters in the string before writing it to a
>> file. (The package is supposed to be writing a sed script, so I need to
>> escape characters that have a special meaning in regular expressions.)
>>
>
> Can't sed do this itself?
In two passes, obviously yes. (Pass the sed script itself through a
simple sed command to escape the chars, then run the escaped script).
I'd be OK with this for my own personal use, but it's a bit ugly in a
package that may be used by others.
Originally, I assumed there must be an easy way to get sed to do a
simple substitution of one sequence of characters by another, without
any regexp processing. Then it occurred to me that in all my years of
using sed, I'd never needed to do this (because I could always just
escape the special characters manually in the regexp). Having read the
sed man pages again, it seems there's no sed command that does simple
substitution (unless I'm missing something obvious - always possible!).
> I'm almost afraid to mention this, but
> perl has a builtin function called quotemeta which does exactly what you
> want.
Thanks for that. I guess I could use perl instead (I'd have to learn it
first!). But sed is probably slightly more widespread than perl. I'm not
fixated on sed though. Maybe it would be simpler in awk, or perhaps even
m4 would be able to do it!? (Not sure I want to go there - coding m4 is
probably an even bigger brainf*ck than TeX!)
Still, it feels like TeX should be able to do what I want. After all, it
is a macro language designed specifically for processing text...
> Please, please, please, no language wars. It's just a possible
> option for Tony.
"Toby", in fact :)
More information about the texhax
mailing list