Home > Archive > PERL Miscellaneous > May 2005 > regexp replace problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
regexp replace problem
|
|
| Kasper 2005-05-29, 3:56 pm |
| 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
| |
| Gunnar Hjalmarsson 2005-05-29, 3:56 pm |
| Kasper wrote:
> 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.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Fabian Pilkowski 2005-05-29, 3:56 pm |
| * Kasper schrieb:
>
> 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
| |
| Tad McClellan 2005-05-29, 8:56 pm |
| Kasper <kcecek@gmail.com> wrote:
> 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?
[color=darkred]
> The result of presented lines should be :
>
> aaabbcc,aaabbdd,aaabbee
> aaabbcc,aaabbee,aaabbff,aaabbgg,aaabbhh[
/color]
-------------------
#!/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";
}
-------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Kasper 2005-05-30, 8:57 am |
| 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
| |
| Fabian Pilkowski 2005-05-30, 8:57 am |
| * Kasper schrieb:
>
> 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
| |
| Tad McClellan 2005-05-30, 3:57 pm |
| Kasper <kcecek@gmail.com> wrote:
> Thanks for reply
What reply?
Please quote some context in followups like everybody else does.
> but I think that my post wasn't enough precise.
Your post was precise enough to be solved in other followups.
> I was thinking about only replace command.
Perl does not have "commands", and nothing named "replace"...
> s/,/,aaa/
.... that is Perl's "substitution operator", not a "replace command".
> I was thinking about gathering somehow the first element maybe like
> this (?=^[^,]+) and use it in replace for ",".
Why not think about using one of the solutions already provided?
If there is some reason why those will not work for you,
then *tell us* that reason so that we can help you overcome it.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
|
|
|