3
\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} 

Using this code I'm getting match & match group one after one.

Example

\curl{https://www.facebook.com/reel/1a1c6e99h60a3169h86816}

Output is enter image description here

Where First /1a1c6e99h6 is match & second half is match group 1a1c6e99h6

How can I only map match group 1a1c6e99h6

5
  • Can you give us a compilable code?
    – projetmbc
    Commented Feb 15, 2023 at 8:28
  • Have you tried to pop the first element via \seq_pop_left before iterating on the sequence? The doc says : "If it exists, the match is stored as the first item of the ⟨seq var⟩, and further items are the contents of capturing groups, in the order of their opening parenthesis. "
    – projetmbc
    Commented Feb 15, 2023 at 8:32
  • That's the only code I'm using. Also \seq_pop_left is not working
    – d4rkshad0w
    Commented Feb 15, 2023 at 8:47
  • Gives us a code with \begin{document}... etc. This will allow us to copy and paste your code in texlive.net/run to quickly try something...
    – projetmbc
    Commented Feb 15, 2023 at 8:48
  • Added the code.
    – d4rkshad0w
    Commented Feb 15, 2023 at 8:54

2 Answers 2

5

Here is how to pop left the sequence such as to obtain 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} 
1
  • 1
    Thanks it worked.
    – d4rkshad0w
    Commented Feb 15, 2023 at 9:10
3

I think you don't want extraction, but just replacement:

\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} 

enter image description here

You must log in to answer this question.

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