Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Stupid regex problem...

Reply
Thread Tools

Stupid regex problem...

 
 
Leif K-Brooks
Guest
Posts: n/a
 
      11-02-2003
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;
}

 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      11-02-2003
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$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-02-2003
In article <1f7pb.203$(E-Mail Removed)>, 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
 
Reply With Quote
 
Leif K-Brooks
Guest
Posts: n/a
 
      11-02-2003
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
stupid question...waiting for a stupid answer Brandon McCombs Java 4 08-28-2006 06:57 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
Stupid is as Stupid Does! Michael P Gabriel Digital Photography 3 06-26-2004 12:49 PM



Advertisments