Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > get the matching regex pattern

Reply
Thread Tools

get the matching regex pattern

 
 
Ram Prasad
Guest
Posts: n/a
 
      03-20-2008
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

http://pragatee.com
 
Reply With Quote
 
 
 
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      03-20-2008
On Mar 20, 6:16 am, Ram Prasad <ramprasad...@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 ?
>


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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-20-2008
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
 
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
Regex testing and UTF8 awarenes or Regex and numeric pattern matching sln@netherlands.com Perl Misc 2 03-10-2009 03:51 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Python 8 09-26-2006 03:24 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Perl Misc 2 09-25-2006 03:15 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57