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 docs at http://code.google.com/p/minify/wiki/UriRewriting $min_serveOptions['rewriteCssUris'] = false; I should be able to add
According to this after installing resharper code completion should looks like this but what
According to msdn it should be in windbase.h but it is not available instead
According to following code if $host_name is something like example.com PHP returns a notice:
According to the Scala documentation for the method Range.end , it returns the exclusive
According to the MSDN help for VB6 Floating-point values can be expressed as mmmEeee
According to https://github.com/mperham/dalli , we can configure multiple Memcache servers. But I'm not sure
According to BeautifulSoup documentation , it is possible to get the value of tag's
According to my previous question integration of jsf2.0 and spring 3.1 and hibernate 4.1
According to the C++11 standard, is the following program well-formed and portable C++? int

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.