2

I admit that I am new to regex expressions. The l3regex module has a function \regex_match: that tests whether the regex (1st arg) matches any part of the token list (2nd arg). I'm curious if there is a function that tests whether the regex matches the entire, whole token list instead of a partial submatch(es).

For example, let's say that I have a function that tests whether the input takes the form of either exactly 1 digit or exactly 2 digits. To knowledge, my first example function will correctly test whether this is true, but I feel this should be easier. (ERROR is just a placeholder error message.) My second example function will unfortunately set that the regex matches any token list with at least 1 digit, even if the token list cannot any non-digits (e.g. a letter).

\documentclass{article}
\begin{document}
\ExplSyntaxOn
%first example function
%\cs_new:Nn \mymodule_checkiftwodigits:n
%{
%   \str_set:Nx \l_tmpa_str { #1 }
%   \int_compare:nNnT { \str_count:N \l_tmpa_str } > { 2 }
%   {  ERROR  }
%   \str_map_inline:Nn \l_tmpa_str
%   {
%       \regex_match:nnTF { \d } { ##1 }
%       {  MATCH  }
%       {  NO~ MATCH  }
%   }
%}
%second example function
\cs_new:Nn \mymodule_checkiftwodigits:n
{
    \regex_match:nnTF { \d{1,2} } { #1 }
    {  MATCH  }
    {  NO~ MATCH  }
}
%\mymodule_checkiftwodigits:n {abc }
\mymodule_checkiftwodigits:n {123}%incorrectly matches
\par
\mymodule_checkiftwodigits:n {1a}%incorrectly matches
\par
\mymodule_checkiftwodigits:n {abc}%correct
%i understand that the \regex_match: function is working as intended.
\ExplSyntaxOff
\end{document}

1 Answer 1

2

If this was a standard (PCRE) regex, I would suggest anchoring the start and end, i.e. ^\d{1,2}$.

After a brief test, anchoring also works in l3regex. The code you want is:

\cs_new:Nn \mymodule_checkiftwodigits:n
{
    \regex_match:nnTF { ^\d{1,2}$ } { #1 }% first parameter changed
    {  MATCH  }
    {  NO~ MATCH  }
}

When I put this change into your document, it comes up with NO MATCH 3 times. Adding in a line \mymodule_checkiftwodigits:n {56} results in MATCH.

4
  • 1
    It should be \cs_new_protected:Nn. Anchoring at the beginning can be done with \A instead of ^; at the end with \Z instead of $. This fits better in LaTeX, but is optional.
    – egreg
    Commented Jun 7, 2023 at 8:31
  • @egreg, May you explain why this function requires \cs_new_protected:Nn ? Commented Jun 7, 2023 at 14:54
  • 1
    @User23456234 Because \regex_match:nnTF does not work in expansion contexts, so the rule is to use _protected for functions using it.
    – egreg
    Commented Jun 7, 2023 at 14:57
  • 1
    @egreg, thank you for the additional information (in particular the \A and \Z. I know in other SE sites that comments can be cleaned up, should I edit my answer here to include those updates, or just leave it as is, with the comments remaining?
    – enkorvaks
    Commented Jun 7, 2023 at 22:36

You must log in to answer this question.

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