For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > get the matching regex pattern









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author get the matching regex pattern
Ram Prasad

2008-03-20, 7:02 pm

I have a somewhat strange requirement
I want to find if a regex matched what exactly matched

to reproduce this

------------------
my @x;
$x[0] = 'chi+ld*';
$x[1] = '\sjoke';

$_=getinput(); # for test assume $_="This is a joke";

if(/($x[0]|$x[1])/){
print "Matched '$1' \n";
}
-----------------


I want to know if $x[0] matched or $x[1] matched
What is the most efficient way of doing this ?


Thanks
Ram



--
For spammers only
spamme@pragatee.com
http://pragatee.com

Chas. Owens

2008-03-20, 7:02 pm

On Thu, Mar 20, 2008 at 9:01 AM, Ram Pra <rampra.ap@gmail.com> wrote:
> I have a somewhat strange requirement
> I want to find if a regex matched what exactly matched
>
> to reproduce this
>
> ------------------
> my @x;
> $x[0] = 'chi+ld*';
> $x[1] = '\sjoke';
>
> $_=getinput(); # for test assume $_="This is a joke";
>
> if(/($x[0]|$x[1])/){
> print "Matched '$1' \n";
> }
> -----------------
>
>
> I want to know if $x[0] matched or $x[1] matched
> What is the most efficient way of doing this ?

snip

Don't use an alternation:

if (/($x[0])/) {
#first one matched
} elsif (/($x[1])/) {
#second one matched
} else {
#neither matched
}

Also, you should use the qr// operator* instead of quotes when storing
a regex in a variable:

my @x = (
qr/chi+ld*/,
qr/\sjoke/
);

* http://perldoc.perl.org/perlop.html...-Like-Operators
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
Gunnar Hjalmarsson

2008-03-20, 7:02 pm

Ram Pra wrote:
> I want to find if a regex matched what exactly matched
>
> to reproduce this
>
> ------------------
> my @x;
> $x[0] = 'chi+ld*';
> $x[1] = '\sjoke';
>
> $_=getinput(); # for test assume $_="This is a joke";
>
> if(/($x[0]|$x[1])/){
> print "Matched '$1' \n";
> }
> -----------------
>
> I want to know if $x[0] matched or $x[1] matched
> What is the most efficient way of doing this ?


"Efficient", in what sense?

Anyway, you can surround both scalars with capturing parentheses.

my @x = ('chi+ld*', '\sjoke');
$_ = 'This is a joke';
if ( /($x[0])|($x[1])/ ) {
print '$x[', $1 ? '0' : '1', "] matched.\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson

2008-03-20, 7:02 pm

Just posted to clpmisc:

-------- Original Message --------
Subject: Re: get the matching regex pattern
Date: Thu, 20 Mar 2008 18:44:23 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Newsgroups: comp.lang.perl.misc

Ram Pra wrote:
> I have a somewhat strange requirement
> ...


You had posted the same question to the beginners mailing list just a
few minutes before you posted here, and I just spent a few minutes
answering the question there without knowing that you already had been
helped here.

DO NEVER DO THAT AGAIN !!!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Chas. Owens

2008-03-20, 7:02 pm

On Thu, Mar 20, 2008 at 1:30 PM, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
snip
> if ( /($x[0])|($x[1])/ ) {
> print '$x[', $1 ? '0' : '1', "] matched.\n";
> }

snip

Since you cannot necessarily predict what $x[0] will hold, you should
probably be testing $1 for definedness not truth:

#!/usr/bin/perl

use warnings;
use strict;

my @x = (
qr/0/,
qr/foo/
);

$_ = 0;

if (/($x[0])|($x[1])/) {
print '$x[', $1 ? '0' : '1', "] matched.\n";
}
if (/($x[0])|($x[1])/) {
print '$x[', defined $1 ? '0' : '1', "] matched.\n";
}

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
Chas. Owens

2008-03-20, 7:02 pm

On Thu, Mar 20, 2008 at 2:02 PM, John W. Krahn <krahnj@telus.net> wrote:
snip
>
> print "\$x[", @- - 1, "] matched.\n"

snip

That only works if we assume that $x[0] and $x[1] are free of captures
themselves:

#!/usr/bin/perl

use warnings;
use strict;

my @x = (
qr/(0)/,
qr/(foo)/
);

$_ = 0;

if (/($x[0])|($x[1])/) {
print '$x[', $1 ? '0' : '1', "] matched.\n";
}
if (/($x[0])|($x[1])/) {
print '$x[', @- - 1, "] matched.\n";
}
if (/($x[0])|($x[1])/) {
print '$x[', defined $1 ? '0' : '1', "] matched.\n";
}

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
John W. Krahn

2008-03-20, 7:02 pm

Gunnar Hjalmarsson wrote:
> Ram Pra wrote:
>
> "Efficient", in what sense?
>
> Anyway, you can surround both scalars with capturing parentheses.
>
> my @x = ('chi+ld*', '\sjoke');
> $_ = 'This is a joke';
> if ( /($x[0])|($x[1])/ ) {
> print '$x[', $1 ? '0' : '1', "] matched.\n";


print "\$x[", @- - 1, "] matched.\n"

> }



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Attn.Steven.Kuo@Gmail.Com

2008-03-20, 10:02 pm

On Mar 20, 6:01 am, rampra...@gmail.com (Ram Pra) wrote:
> I have a somewhat strange requirement
> I want to find if a regex matched what exactly matched
>
> to reproduce this
>
> ------------------
> my @x;
> $x[0] = 'chi+ld*';
> $x[1] = '\sjoke';
>
> $_=getinput(); # for test assume $_="This is a joke";
>
> if(/($x[0]|$x[1])/){
> print "Matched '$1' \n";}
>
> -----------------
>
> I want to know if $x[0] matched or $x[1] matched
> What is the most efficient way of doing this ?
>



Your question is similar to the one posted here:

http://perldoc.perl.org/perlfaq6.ht...ions-at-once%3f

and thus similarly answered:

my @strings = ('chi+ld*', '\sjoke');
$_ = 'This is a joke';

for my $s (@strings)
{
my $pat = qr/($s)/;
print "Matched '$1' in '$_' (with pattern string '$s')\n" if (/
($pat)/);
}

--
Hope this helps,
Steven

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com