Stupid regex problem...

Discussion in 'Perl Misc' started by Leif K-Brooks, Nov 2, 2003.

  1. I have the following Perl code. When I run it, Perl complains that $1 is
    undefined. If I change (foo)|(bar) to (bar)|(foo), it works fine. I know
    I could just use /(foo|bar)/, but this is just a simplified example.

    use strict;
    use warnings;

    my $text = 'bar';
    if ($text =~ /(?:(foo)|(bar))/) {
    print $1;
    }
     
    Leif K-Brooks, Nov 2, 2003
    #1
    1. Advertising

  2. Also sprach Leif K-Brooks:

    > I have the following Perl code. When I run it, Perl complains that $1 is
    > undefined. If I change (foo)|(bar) to (bar)|(foo), it works fine. I know
    > I could just use /(foo|bar)/, but this is just a simplified example.
    >
    > use strict;
    > use warnings;
    >
    > my $text = 'bar';
    > if ($text =~ /(?:(foo)|(bar))/) {
    > print $1;
    > }


    You have two capturing pairs of parens of which only one will (or none
    actually) can match. There is the special variable $+ (see perlvar.pod)
    which holds the actual match:

    my $text = 'bar';
    if ($text =~ /(?:(foo)|(bar))/) {
    print $+;
    }
    __END__
    bar

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Nov 2, 2003
    #2
    1. Advertising

  3. In article <1f7pb.203$>, Leif K-Brooks wrote:
    > I have the following Perl code. When I run it, Perl complains that $1 is
    > undefined. If I change (foo)|(bar) to (bar)|(foo), it works fine. I know
    > I could just use /(foo|bar)/, but this is just a simplified example.
    >
    > use strict;
    > use warnings;
    >
    > my $text = 'bar';
    > if ($text =~ /(?:(foo)|(bar))/) {
    > print $1;
    > }
    >


    You never actually posed a question, so I will assume that you
    meant "Why does is $1 undefined?".

    Well, the regex as a whole matches, and the part that matched
    the first parenthesis is stored in $1 and the part that matches
    the second parentesis is stored in $2. Clearly, nothing matches
    the first parenthesis, so $1 will be undefined.


    --
    Andreas Kähäri
     
    Andreas Kahari, Nov 2, 2003
    #3
  4. Tassilo v. Parseval wrote:


    > You have two capturing pairs of parens of which only one will (or none
    > actually) can match.


    Ok, that makes sense. I was assuming the parens would only store if they
    were the ones used in the alternation. Is there any way to make that happen?
     
    Leif K-Brooks, Nov 2, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Koos Pol

    Stupid regex problem

    Koos Pol, Apr 5, 2006, in forum: Java
    Replies:
    9
    Views:
    374
    Stefan Ram
    Apr 7, 2006
  2. Brandon McCombs
    Replies:
    4
    Views:
    529
    Richard Wheeldon
    Aug 28, 2006
  3. Replies:
    3
    Views:
    809
    Reedick, Andrew
    Jul 1, 2008
  4. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,049
  5. Jason C
    Replies:
    2
    Views:
    443
    Rainer Weikusat
    Jul 18, 2012
Loading...

Share This Page