in reply to Re: How to know that a regexp matched, and get its capture groups?
in thread How to know that a regexp matched, and get its capture groups?

> I'm confused...

I need to find the first regexp that is stored in @syntax that matches, and discard the rest. When one matches, I need the capture groups, if any.

But I'll take the other answer, that a regexp returns true even without capture groups. I could not quickly find any mention of return values in either perlre or perlsyn, both rather hefty pages, before asking. Was probably looking in the wrong place anyway... Ah, yes, I found it in perlop now:

Matching in list context If the "/g" option is not used, "m//" in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, that is, ($1, $2, $3...) (Note that here $1 etc. are also set). When there are no parentheses in the pattern, the return value is the list "(1)" for success. With or without parentheses, an empty list is returned upon failure.

I didn't expect this many answers, to be honest...

  • Comment on Re^2: How to know that a regexp matched, and get its capture groups?
  • Download Code

Replies are listed 'Best First'.
Re^3: How to know that a regexp matched, and get its capture groups?
by LanX (Saint) on Jan 10, 2023 at 16:03 UTC
    > I didn't expect this many answers, to be honest...

    Because the problem is not easy to grasp normally one knows beforehand if captures are expected.

    And the documentation is accurate.

    My last solution here should fix the fake capture issue in a straight forward way, without any performance or version penalty.

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      > Because the problem is not easy to grasp normally one knows beforehand if captures are expected.

      Very much true. And for the fake captures issue, the callback knows (is written for) the regexp it is attached to, so it simply ignores any arguments passed. So it is a non-issue in practice.