Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:44:11+00:00 2026-05-22T14:44:11+00:00

According to perlretut … in scalar context, $time =~ /(\d\d):(\d\d):(\d\d)/ returns a true or

  • 0

According to perlretut

… in scalar context, $time =~ /(\d\d):(\d\d):(\d\d)/ returns a true or false value. In list context, however, it returns the list of matched values ($1,$2,$3) .

But I can’t find an explanation of what is returned in list context if the pattern matches when there are no capturing groups in the regexp. Testing shows that it is the list (1) (single element, integer 1). (Ancillary question – will it always be this, where is it defined?)

This makes it difficult to do what I want:

if (my @captures = ($input =~ $regexp)) {
    furtherProcessing(@captures);
}

I want furtherProcessing to be called if there is a match, with any captured groups passed as arguments. The problem comes when the $regexp contains no capturing groups because then I want furtherProcessing to be called with no arguments, not with the value 1 which is what happens in the above. I can’t test for (1) as a special case, like this

if (my @captures = ($input =~ $regexp)) {
    shift @captures if $captures[0] == 1;
    furtherProcessing(@captures);
}

because in the case of

$input = 'a value:1';
$regexp = qr/value:(\S+)/;

there is a captured value in @captures that happens to look the same as what I get when the $regexp matches but has no capturing groups.

Is there a way to do what I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:44:11+00:00Added an answer on May 22, 2026 at 2:44 pm

    You can use $#+ to find out how many groups were in the last successful match. If that’s 0, then there were no groups and you have (1). (Yes, it will always be (1) if there are no groups, as documented in perlop.)

    So, this will do what you want:

    if (my @captures = ($input =~ $regexp)) {
        @captures = () unless $#+; # Only want actual capture groups
        furtherProcessing(@captures);
    }
    

    Note that $#+ counts all groups, whether they matched or not (as long as the entire RE matched). So, "hello" =~ /hello( world)?/ will return 1 group, even though the group didn’t match (the value in @captures will be undef).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to MSDN form.RightToLeftLayout = True; form.RightToLeft = ifWeWantRTL() ? RightToLeft.True : RightToLeft.False; is
According to here , the C compiler will pad out values when writing a
According to W3C standards, if you have a nillable element with a nil value,
According to what I've heard, integer values that are not the processor's word size
According to the documentation , socket_read() is supposed to return FALSE when the remote
According to Microsoft , you must sign your ClickOnce application. But it seems to
According to 8.2 The Null Type chapter of Ecma-262 null is type. But typeof(null)
According to the W3C Recommendation for the CSS background shorthand , the values are
According to this article Silverlight 2 Beta 2 supports the DataContractJsonSerializer object. But, when
According to the MSDN article found at http://msdn.microsoft.com/en-us/library/wyk4d9cy.aspx the floating-point value .1 has no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.