simple regexp problem

S

S.Marion

Hello,

I'm new to perl and have the following problem.
I have patterns such as:
[Ljava/lang/StackTraceElement
that I want to turn into:
Ljava/lang/StackTraceElement[]

To do this I've got the following regexp:
$methodRet =~ s/(\[*)([^\[]*)/$2$1/;

But this doesn't work since it looks like looping endlessly...
Could anyone point out the mistake?

Cheers,

Marion Sebastien
 
U

usenet

S.Marion said:
$methodRet =~ s/(\[*)([^\[]*)/$2$1/;

Your first match gets opening brackets, which I think is your intent. I
believe your intent with the second match is to get the rest of the
string following the open bracket(s). Since you've already matched the
open bracket, you don't need to try to exclude it from the second match
- the second match picks up where the first match left off. So all you
need to do with the second match is to grab everything that remains,
such as:

$method_ret =~ s/(\[*)(.*)/$2$1/;

However, nothing in your code or your sample string will give you a
closing bracket. If you want to insure the bracket is pared (and it
might not be pared in the data) then that's an additional task.

Welcome to Perl and CLPMisc, by the way. You posts are welcome here,
but you may wish to review the posting guidelines for this group, which
you can find here:

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

A. Sinan Unur

I'm new to perl and have the following problem.
I have patterns such as:
[Ljava/lang/StackTraceElement
that I want to turn into:
Ljava/lang/StackTraceElement[]

To do this I've got the following regexp:
$methodRet =~ s/(\[*)([^\[]*)/$2$1/;

But this doesn't work since it looks like looping endlessly...
Could anyone point out the mistake?

In the example data you have provided, there is only one [ at the
beginning. So, you should anchor the regex at the beginning of the
string, look for a [ right there. I don't understand what you are trying
to do in the second capturing group: Match zero or times *will* succeed
when it matches zero times.

#!/usr/bin/perl

use warnings;
use strict;

my $s = '[Ljava/lang/StackTraceElement';

$s =~ s{\A \[ ( \w+ (?: / \w+ )* ) }
{$1\[\]}x;

print "$s\n";

__END__
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top