Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

regex to reverse string

by jai (Sexton)
on Aug 29, 2003 at 14:27 UTC ( [id://287669]=perlquestion: print w/replies, xml ) Need Help??

jai has asked for the wisdom of the Perl Monks concerning the following question:

Is there any regex to reverse the characters of a word ? Given text it should produce txet. I am trying to implement the linux command 'tac' in perl..

jai..

Replies are listed 'Best First'.
Re: regex to reverse string
by broquaint (Abbot) on Aug 29, 2003 at 14:30 UTC
    Instead of a regex, just use reverse e.g
    my $str = reverse 'a string'; print $str; __output__ gnirts a
    Make sure you're using it in scalar context though if you want to reverse a string. See. the reverse docs for more info.
    HTH

    _________
    broquaint

Re: regex to reverse string
by diotalevi (Canon) on Aug 29, 2003 at 16:20 UTC

    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:.))" }
Re: regex to reverse string
by Zaxo (Archbishop) on Aug 29, 2003 at 14:33 UTC

    I think reverse in scalar context is what you want.

    my $foo = 'text'; my $oof = reverse $foo; print $oof, $/;
    A regex might be possible but is unnecessary.

    After Compline,
    Zaxo

Re: regex to reverse string
by Fletch (Bishop) on Aug 29, 2003 at 14:46 UTC
(z) Re: regex to reverse string
by zigdon (Deacon) on Aug 29, 2003 at 14:35 UTC
    Any particular reason you want to use a regex? The reverse command might work for you:
    print scalar reverse $string;

    -- zigdon

Re: regex to reverse string
by antirice (Priest) on Aug 29, 2003 at 14:46 UTC

    <tongue_in_cheek>Sure, I'll bite: s/(.*)/reverse($1)/e </tongue_in_cheek>

    Hope this helps, although I doubt it will.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: regex to reverse string
by artist (Parson) on Aug 29, 2003 at 16:26 UTC
    Now you can make practice of checking perldoc and find solution to many such problem along your journey.

    For your case check perldoc -q reverse or perldoc -f reverse.

    For more information, read perldoc perldoc.

    artist

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found