Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > reverse-function with regex

Reply
Thread Tools

reverse-function with regex

 
 
Pieter Online
Guest
Posts: n/a
 
      01-25-2007
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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      01-25-2007
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.
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      01-25-2007
Michele Dondi <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> On 25 Jan 2007 00:56:13 -0800, "Pieter Online"
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      01-25-2007
On 25 Jan 2007, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Pieter Online
Guest
Posts: n/a
 
      01-25-2007
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 <(E-Mail Removed)> wrote:
> On 25 Jan 2007, (E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments