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