GL <
goua...@gmail.com> a écrit:
Try this:
\def\endcheck{\endcheck}
\def\checkhash#1{%
\docheckhash#1\endcheck
}
\def\docheckhash#1{%
\ifx#1\endcheck
\expandafter\second
\else
\ifx#1##%
\expandafter\expandafter\expandafter\gobbletocheck
\else
\expandafter\expandafter\expandafter\docheckhash
\fi
\fi
}
\def\second#1#2{#2}
\def\gobbletocheck#1\endcheck#2#3{#2}
\checkhash{abc#def}{YES}{NO}
\checkhash{abcdef}{YES}{NO}
\bye
This will run into trouble if the string to test contains groups with
the first two tokens repeated, e.g.:
\checkhash{ab{cc}def}{YES}{NO} % This will hang indefinitely.
If you’re interested in the hash character, no matter its catcode, the
following definition is more robust (only two lines have changed):
\def\endcheck{\endcheck}
\def\checkhash#1{%
\docheckhash#1\endcheck
}
\csname my hash string: #\endcsname
\def\docheckhash#1{%
\ifx#1\endcheck
\expandafter\second
\else
\ifcsname my hash string: \detokenize{#1}\endcsname
\expandafter\expandafter\expandafter\gobbletocheck
\else
\expandafter\expandafter\expandafter\docheckhash
\fi
\fi
}
\def\second#1#2{#2}
\def\gobbletocheck#1\endcheck#2#3{#2}
Otherwise, if the test needs not be expandable, other solutions can be found.
Best,
Paul