# Regex for a string with a command inside it

I have managed to create a command that takes an argument and does substitutions on it with l3regex. However, if the argument contains itself a command, the command name is printed in the output as if it was a string.

\documentclass{article}
\usepackage{l3regex}
\ExplSyntaxOn
\newcommand\mycommand[1]{
\str_set:Nn \l_temp_str {#1}
\regex_replace_all:nnN {[abc]} {\c{textbf}\cB\{\0\cE\}} \l_temp_str
{\l_temp_str}
}
\ExplSyntaxOff

\begin{document}
\mycommand{wax\textit{b}ycz}
\end{document}


How should I correct the code so that the argument string can include any command?

## 1 Answer

Try using a token list rather than a string:

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}
{
\tl_set:Nn \l_tmpa_tl { #1 }
%\str_set:Nn \l_temp_str {#1}
\regex_replace_all:nnN {[abc]} {\c{textbf}\cB\{\0\cE\}} \l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff

\begin{document}
\mycommand{wax\textit{b}ycz}
\end{document}


This should treat commands as commands.

• I've fixed a few slips, hope you don't mind. – egreg Jun 13 '16 at 21:00