Using eval in regex 
Author Message
 Using eval in regex

I'm trying to write a regex matching substrings with reversed
substrings :

xxxabcdexxxedcbaxxx
     _____     _____

Do you know if I can use eval in a regular expression for that ?

if ( /(\w+)\w{1,10}(eval(reverse \1))/g ) {
  print "match $1 \n";

Quote:
}

else {
  print " no match \n";

Quote:
};

It does not seem to work. Any ideas ?


Sat, 29 Jan 2005 19:05:26 GMT  
 Using eval in regex

Quote:

> I'm trying to write a regex matching substrings with reversed
> substrings :

> xxxabcdexxxedcbaxxx
>      _____     _____

> Do you know if I can use eval in a regular expression for that ?

> if ( /(\w+)\w{1,10}(eval(reverse \1))/g ) {
>   print "match $1 \n";
> }
> else {
>   print " no match \n";
> };

> It does not seem to work. Any ideas ?

You could try the "highly experimental" extended regex feature (as
explained in perlre):

while (<DATA>) {
    if (/(\w+)(?:\w*?)(??{scalar reverse $1})/) {
        print "$1\n";
    }

Quote:
}

__DATA__
This_is_in_perlre_si_sihT
in_perlre_try_perldoc_erlrep_ni
xxxabcdexxxedcbaxxx

--
Cheers,
Steven



Sat, 29 Jan 2005 20:11:03 GMT  
 Using eval in regex
Thanks a lot Steven, it worked great.
By the way, why does this regex requires $1 and not /1 ?
I thought that $1  works only outside of regex.

On Tue, 13 Aug 2002 12:11:03 -0700, Steven Kuo

Quote:


>> I'm trying to write a regex matching substrings with reversed
>> substrings :

>> xxxabcdexxxedcbaxxx
>>      _____     _____

>> Do you know if I can use eval in a regular expression for that ?

>> if ( /(\w+)\w{1,10}(eval(reverse \1))/g ) {
>>   print "match $1 \n";
>> }
>> else {
>>   print " no match \n";
>> };

>> It does not seem to work. Any ideas ?

>You could try the "highly experimental" extended regex feature (as
>explained in perlre):

>while (<DATA>) {
>    if (/(\w+)(?:\w*?)(??{scalar reverse $1})/) {
>    print "$1\n";
>    }
>}

>__DATA__
>This_is_in_perlre_si_sihT
>in_perlre_try_perldoc_erlrep_ni
>xxxabcdexxxedcbaxxx



Sat, 29 Jan 2005 21:21:40 GMT  
 Using eval in regex

Quote:

> On Tue, 13 Aug 2002 12:11:03 -0700, Steven Kuo


> >> I'm trying to write a regex matching substrings with reversed
> >> substrings :
> >> ...
> >> It does not seem to work. Any ideas ?

> >You could try the "highly experimental" extended regex feature (as
> >explained in perlre):

> >while (<DATA>) {
> >    if (/(\w+)(?:\w*?)(??{scalar reverse $1})/) {
> >       print "$1\n";
> >    }
> >}
> > ...

> Thanks a lot Steven, it worked great.
> By the way, why does this regex requires $1 and not /1 ?
> I thought that $1  works only outside of regex.

$1 is determined at run time.  The code which produces the regular
expression is also evaluated at run time.  That's why we use $1
instead of \1.

The experts in c.l.p.m. can expand upon that explanation (for my
benefit as well)...

--
Cheers,
Steven



Sat, 29 Jan 2005 22:23:49 GMT  
 Using eval in regex

Quote:

[...]
> > Thanks a lot Steven, it worked great.
> > By the way, why does this regex requires $1 and not /1 ?
> > I thought that $1  works only outside of regex.

> $1 is determined at run time.  The code which produces the regular
> expression is also evaluated at run time.  That's why we use $1
> instead of \1.

> The experts in c.l.p.m. can expand upon that explanation (for my
> benefit as well)...

Hrm, well, expert, no. By saying that you'll scare off everybody who doesn't
consider himself an expert.
Thanks for the compliment anyway. ;)

Anyway, I generally advise $1 over \1 because...

perldoc perlre
---
Warning on \1 vs $1

Some people get too used to writing things like:

    $pattern =~ s/(\W)/\\\1/g;

This is grandfathered for the RHS of a substitute to avoid shocking the sed
{*filter*}s, but it's a dirty habit to get into. That's because in PerlThink,
the righthand side of a s/// is a double-quoted string. \1 in the usual
double-quoted string means a control-A. The customary Unix meaning of \1 is
kludged in for s///. However, if you get into the habit of doing that, you
get yourself into trouble if you then add an /e modifier.

    s/(\d+)/ \1 + 1 /eg;        # causes warning under -w

Or if you try to do

    s/(\d+)/\1000/;

You can't disambiguate that by saying \{1}000, whereas you can fix it with
${1}000. The operation of interpolation should not be confused with the
operation of matching a backreference. Certainly they mean two different
things on the left side of the s///.
---

Does that clear things up?

Steffen





Sat, 29 Jan 2005 23:42:29 GMT  
 Using eval in regex

Quote:

>By the way, why does this regex requires $1 and not /1 ?
>I thought that $1  works only outside of regex.

Yes, and no. \1 is to be used only in plain sections in the regex. But
(??{...}) is special, as what is inside it is not a plain regex section,
but code (thus requiring $1, \1 will not work there) which evaluates to
something that gets compiled into a regex. So if this code produced
something contain a backslash followed by a "1", it normally would work
there (exception: when the backslash itself got escaped).

BTW I don't expect that this will do what you want. You don't set the
minimum requirement for the length of the delimiter and its reverse
higher than one. So if you attempt to apply your thing on
"xxxabcdexxxxedcbaxxx", it would match an "x" followed by an "x".

--
        Bart.



Tue, 01 Feb 2005 14:34:40 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Test for eval() without using eval() ??

2. s// using lexicals and refs in an eval'ed string

3. Using eval() for subroutine unwind

4. using here document with eval fails

5. Using eval to iterate over a set of Perl variables

6. Using eval

7. Using Eval Statement To Trap Module Not Present Fatal Error

8. using eval to create formats

9. Using eval and $@

10. how to setup exit routine using eval

11. using eval

12. dynamic object instantiation using eval

 

 
Powered by phpBB® Forum Software