Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by LanX (Saint)
on Jan 09, 2023 at 20:34 UTC ( [id://11149472]=note: print w/replies, xml ) Need Help??


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

I'm confused...

> It then occurred to me that the code won't run if the regexp has no capture groups!

Why do you use the if if you don't want to test if there was no @match? (empty list-assignments are false)

and if you need the if b/c you wanna handle a no-match separately, why don't you use an else branch?

edit

OK, I think your problem is that $re can match -hence be true - without any internal syntax defining (capture) groups.

Hence check if (@match) separately, whether with surrounding if for the $re or not depends on the desired logic...

update

after some testing do I like tybalt's solution the most Re: How to know that a regexp matched, and get its capture groups?

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

Replies are listed 'Best First'.
Re^2: How to know that a regexp matched, and get its capture groups?
by Anonymous Monk on Jan 10, 2023 at 15:55 UTC

    > 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...

      > 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2024-04-16 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found