On Mar 20, 6:01 am, [EMAIL PROTECTED] (Ram Prasad) 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.html#How-do-I-efficiently-match-many-regular-expressions-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


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to