reverse-function with regex

Discussion in 'Perl Misc' started by Pieter Online, Jan 25, 2007.

  1. Hello,

    I'm trying to write a reverse-function with regex,

    The following code gives: fedcba
    $message = "abcdef";
    $message =~ s/(.)(.)(.)(.)(.)(.)/$6$5$4$3$2$1/;
    print $message;

    But I like to use something like:
    $message =~ s/(.){6}/$6$5$4$3$2$1/;
    or even
    $message =~ s/(.)*/$6$5$4$3$2$1/;

    But that does not work, and all I can do is hope one of you can help me
    out..

    Thanks in advance,
    Pieter
     
    Pieter Online, Jan 25, 2007
    #1
    1. Advertising

  2. Pieter  Online

    Mirco Wahab Guest

    Pieter Online wrote:
    > Hello,
    >
    > I'm trying to write a reverse-function with regex,
    >
    > The following code gives: fedcba
    > $message = "abcdef";
    > $message =~ s/(.)(.)(.)(.)(.)(.)/$6$5$4$3$2$1/;
    > print $message;
    >
    > But I like to use something like:
    > $message =~ s/(.){6}/$6$5$4$3$2$1/;
    > or even
    > $message =~ s/(.)*/$6$5$4$3$2$1/;
    >
    > But that does not work, and all I can do is hope one of you can help me
    > out..


    $message =~ s/(.*)/reverse $1/eg;

    ;-)

    Regards

    M.
     
    Mirco Wahab, Jan 25, 2007
    #2
    1. Advertising

  3. Pieter  Online

    -berlin.de Guest

    Michele Dondi <> wrote in comp.lang.perl.misc:
    > On 25 Jan 2007 00:56:13 -0800, "Pieter Online"
    > <> wrote:
    >
    > >I'm trying to write a reverse-function with regex,


    [...]

    > BTW: I tried doing the same with a single s/// instead of a pattern
    > match, but it doesn't work:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > sub myownreverse {
    > my $s=shift;
    > $s =~ s/^(.)(.*)/myownreverse($2) . $1/e;
    > $s;
    > }
    >
    > print myownreverse 'abcdef';
    >
    > __END__
    >
    > The latter yields
    >
    > C:\temp>foo.pl
    > ffedcb
    >
    > for me... (And I see no simple modification to correct it.)


    That's because in the substitution part of s/// the call to
    myownreverse() destroys the value of $1 which you are using
    in the same expression. Save the initial value to fix it:

    sub myownreverse {
    local $1;
    my $s=shift;
    $s =~ s/^(.)(.+)/my $x = $1; myownreverse($2) . $x/e;
    $s;
    }

    I have also changed the second capture from (.*) to (.+). If
    (.*) is empty there is nothing to do.

    Anno
     
    -berlin.de, Jan 25, 2007
    #3
  4. Pieter  Online

    Ted Zlatanov Guest

    On 25 Jan 2007, wrote:

    > I'm trying to write a reverse-function with regex,
    >
    > The following code gives: fedcba
    > $message = "abcdef";
    > $message =~ s/(.)(.)(.)(.)(.)(.)/$6$5$4$3$2$1/;
    > print $message;
    >
    > But I like to use something like:
    > $message =~ s/(.){6}/$6$5$4$3$2$1/;
    > or even
    > $message =~ s/(.)*/$6$5$4$3$2$1/;
    >
    > But that does not work, and all I can do is hope one of you can help me
    > out..


    Regular expressions can't reverse arbitrary strings, it's not part of
    their functionality. The only way is to do s/(.*)/reverse($1)/e which
    of course is better written as $message = reverse $message;

    Ted
     
    Ted Zlatanov, Jan 25, 2007
    #4
  5. Thank you guys,
    you're amazing!!!

    The recursive function was great and you're explanation really helped
    me to improve my regex juggling skillz.

    Best regards,
    Pieter

    On Jan 25, 4:48 pm, Ted Zlatanov <> wrote:
    > On 25 Jan 2007, wrote:
    >
    > > I'm trying to write a reverse-function with regex,

    >
    > > The following code gives: fedcba
    > > $message = "abcdef";
    > > $message =~ s/(.)(.)(.)(.)(.)(.)/$6$5$4$3$2$1/;
    > > print $message;

    >
    > > But I like to use something like:
    > > $message =~ s/(.){6}/$6$5$4$3$2$1/;
    > > or even
    > > $message =~ s/(.)*/$6$5$4$3$2$1/;

    >
    > > But that does not work, and all I can do is hope one of you can help me
    > > out..Regular expressions can't reverse arbitrary strings, it's not part of

    > their functionality. The only way is to do s/(.*)/reverse($1)/e which
    > of course is better written as $message = reverse $message;
    >
    > Ted
     
    Pieter Online, Jan 25, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt_C [MVP]
    Replies:
    0
    Views:
    1,918
    Curt_C [MVP]
    Jan 22, 2004
  2. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  3. manuel
    Replies:
    3
    Views:
    272
    Roy Smith
    Sep 11, 2004
  4. Replies:
    3
    Views:
    773
    Reedick, Andrew
    Jul 1, 2008
  5. Mi Ni
    Replies:
    2
    Views:
    161
    Mi Ni
    Jan 31, 2008
Loading...

Share This Page