6

Using regex matching doesn't work as I would expect on variables, though regex replace works just fine under similar circumstances. Why? How does one match to a tl? (similar attempts with str also don't work).

\documentclass{article}
\setlength{\parindent}{0pt}
\begin{document}
  \ExplSyntaxOn 
  This~works:\par
  \regex_match:nnTF {iss}{Mississippi}
    {\quad Match!\\}
    {\quad No~match\\}
    
  This~doesn't~work:\par
  \tl_set:Nn \l_tmpa_tl { Mississippi }
  \regex_match:nnTF {iss}{\l_tmpa_tl}
    {\quad Match!\\}
    {\quad No~match\\} 

  This~also~doesn't~work:\par
    \tl_set:Nn \l_tmpa_tl { Mississippi }
    \regex_match:nnTF {iss}\l_tmpa_tl
      {\quad Match!\\}
      {\quad No~match\\} 

  But~replace~works~just~as~expected:\par
    \tl_set:Nn \l_tmpa_tl { Mississippi } 
    \regex_replace_all:nnN{i}{y}\l_tmpa_tl 
    \quad \l_tmpa_tl\\
  \ExplSyntaxOff
\end{document}

1 Answer 1

7

In general, you shouldn't assume that expl3 will expand any n-type arguments. Instead, you should use a V-type argument. For the case of \regex_match:nnTF, you'll need to generate this variant yourself.

\documentclass{article}
\setlength{\parindent}{0pt}
\begin{document}
  \ExplSyntaxOn
  % \cs_generate_variant:Nn \regex_match:nnTF { nVTF }
  \prg_generate_conditional_variant:Nnn \regex_match:nn { nV } { TF, T, F }

  This~works:\par
  \regex_match:nnTF {iss}{Mississippi}
    {\quad Match!\\}
    {\quad No~match\\}

  This~didn't~work (but~ now~ it~ does):\par
  \tl_set:Nn \l_tmpa_tl { Mississippi }
  \regex_match:nVTF {iss}{\l_tmpa_tl}
    {\quad Match!\\}
    {\quad No~match\\}

  But~replace~works~just~as~expected:\par
    \tl_set:Nn \l_tmpa_tl { Mississippi }
    \regex_replace_all:nnN{i}{y}\l_tmpa_tl
    \quad \l_tmpa_tl\\
  \ExplSyntaxOff
\end{document}

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .