I would like to match the pattern "start with >>" in a string and replace this with a command; and if multiple >> appear, it should match and replace multiple times.
For example, given the string aaa>>bbb>>ccc, I would like to convert it into aaa\somecommand{bbb}\somecommand{ccc}. My idea is to use \regex_replace_all:nnN, but I cannot seem to find the correct regex pattern: for example, >>(.*) would only match once for this string. How to properly achieve this?
Below is a MWE:
\documentclass{article}
\begin{document}
\NewDocumentCommand \somecommand { m }
{
\begin{center} #1 \end{center}
}
\ExplSyntaxOn
\NewDocumentCommand \replace { m }
{
\tl_set:Nn \l_tmpa_tl { #1 }
\regex_replace_all:nnN
{ >> ((?:.(?:?!>>))*.) } % needs to be changed
{ \c{somecommand}{\1} }
\l_tmpa_tl
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff
\replace{aaa>>bbb>>ccc}
% expected: aaa\somecommand{bbb}\somecommand{ccc}
\end{document}


\regex_replace_all:nnNis that you only need to match your replacement once, not multiple times. But you need to make sure that>>can't be part of your regular expression. If no other>are part of it you could simply use>>([^>]*). – Skillmon Aug 28 '23 at 14:28