Why does `xstring`s `\StrBefore` work, but not `\IfBeginWith`?

showkeys makes things safe with \meaning so that labels with & or $ or other special character do not blow up when typeset. so you are comparing catcode 12 ("other") eq with catcode 11 (letter) eq and these are not equal.


Regular expressions in expl3 are category code agnostic.

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{showkeys}

\newtheorem{lemma}{Lemma}

\ExplSyntaxOn
\NewDocumentCommand{\beginwithTF}{mmmm}
 {% #1 = string to search in, #2 = string to search, #3 = true, #4 = false
  \regex_match:nnTF { \A #2\: } { #1 } { #3 } { #4 }
 }
\ExplSyntaxOff

\renewcommand*\showkeyslabelformat[1]{%
  \fbox{\small\ttfamily\beginwithTF{#1}{eq}{equation}{no equation}}}

\begin{document}

\begin{lemma} \label{lemma:B}
\begin{equation} \label{eq:B}
    \sum_{i=0}^\infty 2^{-1} = 2
\end{equation}
\end{lemma}

\end{document}

With \regex_match:nnTF we look whether the given regular expression (first argument) matches the given string (second argument). I've kept the order of arguments in \IfBeginWith, though.

enter image description here