Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: regex to reverse string

by diotalevi (Canon)
on Aug 29, 2003 at 16:20 UTC ( [id://287742]=note: print w/replies, xml ) Need Help??


in reply to regex to reverse string

Just to be perverse, here is exactly what you asked for

print rx_reverse( shift ); sub rx_reverse { local $_ = shift; my $length = length; for my $offset ( 0 .. int($length / 2) ) { my $rx = rx_gen( $length, $offset ); s/$rx/$1$4$3$2/; } $_ } sub rx_gen { my $length = shift; my $offset = shift; my $skip = $length - 2 * ($offset + 1); return unless $length > 0 and $offset <= ($length / 2) and $skip >= 0; $offset = $offset ? "((?s:.){$offset})" : '()'; $skip = $skip ? "((?s:.){$skip})" : '()'; return "^$offset((?s:.))$skip((?s:.))" }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found