Click here to get back home

get the matching regex pattern

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
get the matching regex pattern Ram Prasad 03-20-2008
Posted by Ram Prasad on March 20, 2008, 9:16 am
Please log in for more thread options
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

Posted by comp.llang.perl.moderated on March 20, 2008, 12:36 pm
Please log in for more thread options
> 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 ?
>

One way:

if ( / ($x[0]) | ($x[1]) /x ) {
print defined $1 ? "first" : "second";
}

But, this may be more efficient often:

if ( /$x[0]/ ) { print "first" }
elsif ( /$x[1]/) { print "second" }

Ordering alternatives with the most likely
matches in front can greatly increase efficiency
too.

--
Charles DeRykus

Posted by Jim Gibson on March 20, 2008, 1:06 pm
Please log in for more thread options
In article

> 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";

Then why not do us a favor and replace the above line with:

$_ = "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 ?

One way is to capture the individual alternatives and test the
corresponding dollar values:

#!/usr/local/bin/perl
use strict;
use warnings;

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

try("This is a joke");
try("Save the children");

sub try
{
$_ = shift;
if(/(($x[0])|($x[1]))/){
print "Matched '$1' \n";
print "\$2=$2\n" if $2;
print "\$3=$3\n" if $3;
}
}

Output:

Matched ' joke'
$3= joke
Matched 'child'
$2=child

This will not scale very well for many alternatives. For that, you can
inspect the @- array after the match.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Posted by Gunnar Hjalmarsson on March 20, 2008, 1:44 pm
Please log in for more thread options
Ram Prasad 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

Similar ThreadsPosted
Matching neighbouring words of a pattern using Regex August 30, 2004, 4:45 pm
multiple regex pattern matching per line? September 4, 2004, 2:24 pm
pattern matching dynamic strings w/ regex ending in $ problem May 2, 2005, 4:31 pm
Help with Pattern matching. Matching multiple lines from while reading from a file. May 2, 2007, 11:37 pm
Pattern Matching December 15, 2004, 10:51 am
[PATTERN MATCHING] December 17, 2004, 12:14 pm
[PATTERN MATCHING] December 20, 2004, 8:32 am
pattern matching January 26, 2005, 8:10 am
Pattern Matching March 17, 2005, 1:09 pm
pattern matching.... June 27, 2005, 12:00 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap