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 7068783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:21:58+00:00 2026-05-28T05:21:58+00:00

How to test if a capture is defined in Perl regexp matching? #!/usr/bin/env perl

  • 0

How to test if a capture is defined in Perl regexp matching?

#!/usr/bin/env perl
use v5.10;
my $str="foobar
barfoo";

while($str =~ m/(?:(f.*))|(?:(b.*))/g) {
    say "+ $1";
    say "- $2";
}

It outputs

+ foobar
- 
+ 
- barfoo

while what I want is

+ foobar
- barfoo

I.e. if $1 or $2 aren’t matched, it shouldn’t print.

  • 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-28T05:21:59+00:00Added an answer on May 28, 2026 at 5:21 am

    The thing is you aren’t testing if it’s defined, so it’s going to print the +/- either way.

    while($str =~ m/(?:(f.*))|(?:(b.*))/g) {
        say "+ $1" if defined($1);
        say "- $2" if defined($2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a PHP regexp pattern that would: Capture openid if test
I'm trying to use the fisher.test function, which is called from a perl script
I'm trying to use Test::More to unit test Perl functions that print to the
I have a test list that I am trying to capture data from using
Some of the new features of Perl 5.10 and 5.12, such as say, are
Test Driven Development (TDD) and its benefits are well defined. The same can be
I am trying to capture screenshots on test failure using selenium-client and rspec. I
I'm trying to integrate Cobertura into our test environment to be able to capture
I want to capture output of a Perl program and display output data (string
Let's say file test.php looks like this: <?php echo 'Hello world.'; ?> I want

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.