Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

use of built-in function in regex...

by pkumar.pr (Initiate)
on Aug 06, 2010 at 04:48 UTC ( [id://853301]=perlquestion: print w/replies, xml ) Need Help??

pkumar.pr has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Is it possible to use built-in function like "reverse" inside regex.

for example: If there is a string "isasdffdsakk" I want to capture "asdffdsa" by using reverse function inside regex.

$string = 'isasdffdsakk'; if($string =~ /(.*?)(reverse\1)/s){ print $1,$2; }

Thank you.

Replies are listed 'Best First'.
Re: use of built-in function in regex...
by ikegami (Patriarch) on Aug 06, 2010 at 05:03 UTC
    /(.*?)(??{ quotemeta reverse $1 })/s

    Of course, (.*?) can match a zero-length string. And since it's not anchored, it will always match "". Remove the "?".

Re: use of built-in function in regex...
by murugu (Curate) on Aug 06, 2010 at 05:04 UTC
Re: use of built-in function in regex...
by suhailck (Friar) on Aug 06, 2010 at 06:02 UTC
    perl -le '$string = "isasdffdsakk"; if($string =~ /((.+?)(??{quotemeta reverse $2}))/s){ print $1; } ' asdffdsa


    ~suhail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://853301]
Approved by murugu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-18 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found