regexp replace problem

K

Kasper

Hello,
I got a problem with regular expressions.

My input data look like this :

aaa,bbcc,bbdd,bbee
aaa,bbcc,bbee,bbff,bbgg,bbhh

(There could be many elements like (,[^,]*) in one line)

I want to substitute every second and next element with first element.
The result of presented lines should be :

aaabbcc,aaabbdd,aaabbee
aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh

It don't have to be one line substitution.
Please help - I have no idea how to do this and I tried almost everything.

Kasper
 
G

Gunnar Hjalmarsson

Kasper said:
I got a problem with regular expressions.

My input data look like this :

aaa,bbcc,bbdd,bbee
aaa,bbcc,bbee,bbff,bbgg,bbhh

(There could be many elements like (,[^,]*) in one line)

I want to substitute every second and next element with first element.
The result of presented lines should be :

aaabbcc,aaabbdd,aaabbee
aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh

It don't have to be one line substitution.

Besides the simple regex used with the split() function, I wouldn't use
regular expressions for this problem.

perldoc -f split
perldoc -f shift
perldoc -f map
perldoc -f join
Please help - I have no idea how to do this and I tried almost everything.

Trying "almost everything", while having "no idea" how to do it, sounds
utterly stupid.

Please show us 10 or so of your attempts, and somebody may be able to
help you fix your best shot.
 
F

Fabian Pilkowski

* Kasper said:
My input data look like this :

aaa,bbcc,bbdd,bbee
aaa,bbcc,bbee,bbff,bbgg,bbhh

I want to substitute every second and next element with first element.
The result of presented lines should be :

aaabbcc,aaabbdd,aaabbee
aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh

It don't have to be one line substitution.

This should be easy if you do it that way you would do it in mind. Split
your string into an array, except the first element. Then put this first
one in front of each remaining element. That's all.


#!/usr/bin/perl -w
use strict;
while ( <DATA> ) {
my( $first, @a ) = split /,/;
print join ',', map $first.$_, @a;
}
__DATA__
aaa,bbcc,bbdd,bbee
aaa,bbcc,bbee,bbff,bbgg,bbhh


regards,
fabian
 
T

Tad McClellan

Kasper said:
I got a problem with regular expressions.


No you don't.

Surely you can understand the /,/ regex, and that is all that is needed.

My input data look like this :

aaa,bbcc,bbdd,bbee
aaa,bbcc,bbee,bbff,bbgg,bbhh
I want to substitute every second and next element with first element.


"second and next" means the same thing as "second and third".

You must have meant "second and subsequent" or some such?

The result of presented lines should be :

aaabbcc,aaabbdd,aaabbee
aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh


-------------------
#!/usr/bin/perl
use warnings;
use strict;

foreach ( 'aaa,bbcc,bbdd,bbee', 'aaa,bbcc,bbee,bbff,bbgg,bbhh' ) {
my($prefix, @parts) = split /,/;
print join( ',', map $prefix . $_, @parts), "\n";
}
 
K

Kasper

Hi
Thanks for reply but I think that my post wasn't enough precise.
I was thinking about only replace command.
It is quite easy when first element is const and it's known (aaa)
then s/,/,aaa/ is enough.
I was thinking about gathering somehow the first element maybe like
this (?=^[^,]+) and use it in replace for ",".
Kasper
 
F

Fabian Pilkowski

* Kasper said:
Thanks for reply but I think that my post wasn't enough precise.
I was thinking about only replace command.
It is quite easy when first element is const and it's known (aaa)
then s/,/,aaa/ is enough.
I was thinking about gathering somehow the first element maybe like
this (?=^[^,]+) and use it in replace for ",".

Please execute those examples from Tad and me (they're almost the same)
to see what they do. They really do what you want. Assumedly, it is just
one point you miss: the absence of a *complex* regular expression. But
you don't need such one for this simple task, do you?

Be happy and do this one without a regex.

regards,
fabian
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top