![如何从 LaTex 正则表达式匹配中获取组?](https://linux22.com/image/461102/%E5%A6%82%E4%BD%95%E4%BB%8E%20LaTex%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%8C%B9%E9%85%8D%E4%B8%AD%E8%8E%B7%E5%8F%96%E7%BB%84%EF%BC%9F.png)
\documentclass{article}
\usepackage{hyperref}
\usepackage{expl3}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=cyan,
pdftitle={Overleaf Example},
pdfpagemode=FullScreen,
}
\ExplSyntaxOn
\NewDocumentCommand{\curl}{m}{
\tl_set:Nn \l_tmpa_tl {#1}
\regex_extract_once:nnN {\/([a-z0-9]{10})} {#1} \l_uiy_result_seq
\seq_map_inline:Nn \l_uiy_result_seq {\href{#1}{##1}}
}
\ExplSyntaxOff
\begin{document}
\curl{https://www.facebook.com/reel/1a1c6e99h60a3169h86816}
\end{document}
使用此代码我得到一个接一个的匹配和匹配组。
例子
\curl{https://www.facebook.com/reel/1a1c6e99h60a3169h86816}
其中第一场/1a1c6e99h6
是比赛,第二场是比赛组1a1c6e99h6
我如何才能仅映射匹配组1a1c6e99h6
答案1
以下是如何弹出左侧序列以获取1a1c6e99h6
。
\documentclass{article}
\usepackage{hyperref}
\usepackage{expl3}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=cyan,
pdftitle={Overleaf Example},
pdfpagemode=FullScreen,
}
\ExplSyntaxOn
\NewDocumentCommand{\curl}{m}{
\regex_extract_once:nnN {\/([a-z0-9]{10})} {#1} \l_uiy_result_seq
% One good example of use of l_tmpa_tl.
\seq_pop_left:NNTF \l_uiy_result_seq \l_tmpa_tl {
\seq_map_inline:Nn \l_uiy_result_seq {\href{#1}{##1}}
}{
No ~ match!
}
}
\ExplSyntaxOff
\begin{document}
\curl{https://www.facebook.com/reel/1a1c6e99h60a3169h86816}
\curl{PB}
\end{document}
答案2
我认为您不想提取,而只是替换:
\documentclass{article}
\usepackage{hyperref}
\usepackage{expl3}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=cyan,
pdftitle={Overleaf Example},
pdfpagemode=FullScreen,
}
\ExplSyntaxOn
\NewDocumentCommand{\curl}{m}
{
\tl_set:Nn \l_tmpa_tl {#1}
\regex_replace_once:nnN {.*\/([a-z0-9]{10}).*} {\1} \l_tmpa_tl
\href{#1}{\l_tmpa_tl}
}
\ExplSyntaxOff
\begin{document}
\curl{https://www.facebook.com/reel/1a1c6e99h60a3169h86816}
\end{document}