I have a need to pass through potentially arbitrary characters untouched and found this macro:
\makeatletter
\def\test#1{\expandafter\zap@space\detokenize{#1} \@empty}
\makeatother
The problem is that \detokenize inserts spaces after commands in the expansion so I get rid of these with LaTeX's \zap@space. Unfortunately, I need to keep any spaces which \detokenize did not produce. I suspect that there is some cunning way to do this by redefining the catcode of the space character but it is somewhat beyond me.
For example (yes, it's regular expressions I need to pass through ...),
\test{\A\d{2,}.+\z}
Should expand to \A\d{2,}.+\z
but
\test{A test}
should expand to A test and not lose the space that was there originally.
I should say, that \verb doesn't work as these regexp strings are the value of a keyval pair:
\macro[key=\test{#1}]
Update It seems too hard to do this in general without making other things rather messy so I've settled for requiring that any regexps used are canonicalised to use no literal spaces in them. It's always possible to replace them with '\x20' for example in anyway so this means that \zap@space will only be zapping spaces created by \detokenize, which is fine. Many thanks for all of the answers though as they are very instructive.
\verb|\A\d{2,}.+\z|would be sufficient. – egreg Feb 13 '12 at 13:42\testmacro in a way that allows to declare the regexes before using them?\declareregex\myregex{\A\d{2,}.+\z}(which reads the argument verbatim) and then\macro[key=\test{\myregex}]– Martin Scharrer Feb 13 '12 at 14:07l3regexprovides that... of course, it is pretty slow). – Bruno Le Floch Feb 13 '12 at 18:34