# How can I match a control sequence using \regex_match?

How can I match a control sequence using \regex_match? This code

\newcommand{\iw}[1]{\immediate\write16{#1}}

\iw{1 should be true}
\regex_match:nnTF {May} {May} {\iw{TRUE}} {\iw{FALSE}}

\newcommand{\z}{May}
\iw{2 should be true}
\regex_match:nnTF {May} {\z} {\iw{TRUE}} {\iw{FALSE}}


prints

1shouldbetrue
TRUE
2shouldbetrue
FALSE


and I want it to print

1shouldbetrue
TRUE
2shouldbetrue
TRUE


## 1 Answer

I'm not very familiar with l3regex so can't comment on whether there is a better/more appropriate way of doing what you want (aside from the fact it doesn't appear that regular expressions are required). However, I believe the problem with the above code is that the macro

\regex_match:nnTF {May} {\z} {\iw{TRUE}} {\iw{FALSE}}


is comparing May to \z, rather than comparing May to the content of \z as you intend. This is because the nn part of the argument specifier says to do nothing with the arguments. What you want is to replace the second argument \z with its replacement text before doing the comparison. This can be accomplished by using a V argument type.

Again, without familiarity with l3regex I can't say whether there is a better "built in" way of doing this, but one option is to use expl3 to generate a variant with the correct argument type. I did that below with

\cs_generate_variant:Nn \regex_match:nnTF {nV}


after which you can call

\regex_match:nVTF {May} \z {\iw{TRUE}} {\iw{FALSE}}


when \z is replaced with its value before the comparison is made.

In the specific example of your above code, the same can be accomplished without regular expressions using one of

\tl_if_in:NnTF \z {May} {\iw{TRUE}} {\iw{FALSE}}
\cs_generate_variant:Nn \tl_if_in:nnTF {nV}
\tl_if_in:NnTF {May} \z {\iw{TRUE}} {\iw{FALSE}}


or,

\cs_generate_variant:Nn \tl_if_eq:nnTF {nV}
\tl_if_eq:nVTF {May} \z {\iw{TRUE}} {\iw{FALSE}}


depending on your intentions.

\documentclass{article}
\usepackage{l3regex}
\ExplSyntaxOn
\newcommand{\iw}[1]{\immediate\write16{#1}}

\iw{1 should be true}
\regex_match:nnTF {May} {May} {\iw{TRUE}} {\iw{FALSE}}

\newcommand{\z}{May}
\iw{2 should be true}

\cs_generate_variant:Nn \regex_match:nnTF {nV}
\regex_match:nVTF {May} \z {\iw{TRUE}} {\iw{FALSE}}

\ExplSyntaxOff

\begin{document}
\end{document}

• l3regex never expands macros; it just compare token lists independently from their meaning. One can specify control sequence names in a regex with \c{foo} to mean \foo; however, even if \foo and \baz are “\ifx-equivalent”, the regex \c{foo} will not match \c{baz}. Your method of generating a variant is what's needed in this very particular situation. – egreg Jun 11 '13 at 9:33