This is actually a follow-up question to Replacing a substring from 6 years ago.
I want to replace a substring in a long string. With \ReplaceStr from the xstring-package this works perfectly fine.
However, if I want to nest multiple of such functions, I get the error Use of \@xs@StrSubstitute@@ doesn't match its definition. \ReplaceStrB{\ReplaceStr{aXYbXYc}}
Any remarks and workarounds are warmly appreciated. I know how to substitute strings outside of LaTeX. However, in this particular case I need a LaTeX-only solution.
\documentclass{article}
\usepackage{xstring}
\newcommand{\ReplaceStrX}[1]{\StrSubstitute{#1}{X}{Y}}
\newcommand{\ReplaceStrB}[1]{\StrSubstitute{#1}{b}{B}}
\begin{document}
\ReplaceStrX{aXbXc} % produces aYbYc
\ReplaceStrB{aXbXc} % produces aXBXc
\ReplaceStrB{\ReplaceStrX{aXbXc}} % should produce aYBYc
\end{document}
EDIT 1: The simple solution
Simply add \noexpandarg\exploregroups as suggested by Manuel.
This worked for my problem. In other cases, also check out Manuel's macro-solution or cfr's 13regex-solution.
EDIT 2: A related question
The following successfully produces aXBXc
\ReplaceStrB{aXbXc}
However this does not work.
\def\tempvar{aXbXc}
\ReplaceStrB{\tempvar}
Any idea, how to safe \tempvar as a string?

minimalfor examples. – cfr Mar 29 '17 at 22:42