# Replacing Multiple Characters Including Backslash

I am in a situation to replace many characters from a sentence. In the working example attached below, command relaceB replaces backslash and command replaceA replaces many other characters like **, /* etc. But I am unable to combine both the commands together to form a single new command (which replaces- *,/,\ etc). Does anybody know how to fix it?

\documentclass{article}
\usepackage{xparse}

%Removing other characters
\ExplSyntaxOn
\tl_new:N \l_azor_string_tl
\cs_new_protected:Npn \azor_replace_norsk:n #1
{
\tl_set:Nn \l_azor_string_tl { #1 }
\tl_replace_all:Nnn \l_azor_string_tl { / } { -- }
\tl_replace_all:Nnn \l_azor_string_tl { ? } { -- }
\tl_replace_all:Nnn \l_azor_string_tl { * } { --}
\tl_use:N \l_azor_string_tl
}
\NewDocumentCommand \replaceA { m }
{
\azor_replace_norsk:n { #1 }
}
\ExplSyntaxOff

%Removing backslash
\ExplSyntaxOn
\NewDocumentCommand{\replaceB}{m}
{
\tl_set:Nn \l_tmpa_tl { #1 }
\regex_replace_all:nnN { \cC. } { \c{cs_to_str:N} \0 } \l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff

\begin{document}
\replaceA{a/b*c} %Works

\replaceB{d\e} %Works

\replaceA{\replaceB{a/b*cd\e}} %Does not work
\end{document}

• Do you mean combining them as in \replace which does \replaceA{\replaceB{#1}} or as in you want them to work in this nested way? Nov 11, 2018 at 8:43
• @TeXnician, yes, a command which works in a nested way would be more suitable. Nov 11, 2018 at 8:55
• @TeXnician, in the third line, the output does not contain backslash (only "de") but the other characters (/,*) are not removed. Nov 11, 2018 at 8:57
• These commands are not expandable, and they don't expand their arguments, so they can't be easily nested. You could modify them to store their product in macro \replaceresult: \replaceB{a/b*cd\e} does not typeset but stores in \replaceresult. \expandafter\replaceA\expandafter{\replaceresult} will now act on that and the new \replaceresult is what you want. you can also define them to expand once, so you don't need \expandafter. Also, simplest might be to define a third command \replaceC doing all your replacements at once, if you want to stick to your initial way of doing things
– user4686
Nov 11, 2018 at 11:00

Use \regex_replace_all:nnN to do all the business at the same time:

\documentclass{article}
\usepackage{xparse}

%Removing other characters
\ExplSyntaxOn
\tl_new:N \l_azor_string_tl
\cs_new_protected:Npn \azor_replace_norsk:n #1
{
\tl_set:Nn \l_azor_string_tl { #1 }
\regex_replace_all:nnN { \cC. } { -- \c{cs_to_str:N} \0 } \l_azor_string_tl
\regex_replace_all:nnN { [/?\*] } { -- } \l_azor_string_tl
\tl_use:N \l_azor_string_tl
}
\NewDocumentCommand \replace { m }
{
\azor_replace_norsk:n { #1 }
}
\ExplSyntaxOff

\begin{document}

\replace{a/b*c}

\replace{d\e}

\replace{a/b*cd\e?f*g}

\end{document}


• thanks, this works, but the backslash is just removed, not replaced by hyphen (--). Nov 11, 2018 at 13:12
• @Ashok Fixed so also the backslash becomes -- Nov 11, 2018 at 13:16