For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > regex substitution









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 regex substitution
Ramprasad A Padmanabhan

2005-04-27, 8:56 am

Hi all,

I want a regex to replace all continuous occurrences of '-' with
something else say 'x' except the first one

These are some examples

"- Ram" ===> "- Ram"
"-- bla" ===> "-x blah"
"bla ---" ===> "bla -xxx"

And also
"-- blah ---" ===> "-x blah -xx"



What I am doing now is a workaround works fine but I think it is an
overkill

--------------------------
# The input string
$x="- -- blah --- blah2";


my $RANDOM='@@@';

# Replace the begining '-'s with @@@
$x=~s/(?<!-)-(?=-*)/$RANDOM/g;

# Replace other '-'s with x
$x=~ s/-/x/g;

# Restore the begining '-'s
$x=~s/$RANDOM/-/g;

print "$x\n";
-----------------------------------------

Thanks
Ram







----------------------------------------------------------
Netcore Solutions Pvt. Ltd.
Website: http://www.netcore.co.in
Spamtraps: http://cleanmail.netcore.co.in/directory.html
----------------------------------------------------------
Ing. Branislav Gerzo

2005-04-27, 8:56 am

Rampra A Padmanabhan [RAP], on Wednesday, April 27, 2005 at 15:55
(+0530) thinks about :

RAP> I want a regex to replace all continuous occurrences of '-' with
RAP> something else say 'x' except the first one

this is nearly what you want, it should be a bit improved:

my @input = ("- Ram", "-- bla", "bla ---", "-- blah ----");
print grep { s/-[^-]*-/-x/g } @input;

--

How do you protect mail on web? I use http://www.2pu.net

[(A)bort (R)etry (C)ontinue ... just kidding.]


Offer Kaye

2005-04-27, 8:56 am

On 4/27/05, Rampra A Padmanabhan wrote:
> Hi all,
>=20
> I want a regex to replace all continuous occurrences of '-' with
> something else say 'x' except the first one
>=20


Here's one way:
s/-(-+)/"-".("x"x length$1)/ge;

Explanation:
Flags: "g" means global, i.e. replace all occurences, and "e" means to
eval the replacement part as a perl expression and use the return
value as the replacement. Read "perldoc perlop", the section on the
s/// operator, for mode details.
First part: "-(-+)" means to match a "-" sign followed by one or more
"-" signs, and save the "-" signs except for the first into $1. Note
that this means that single "-" signs will not be matches and
therefore affected at all.
Second part: returns a "-" (instead of the first, unsaved "-" we
matched in part one), concatanated (that's the ".") with the char "x"
repeated length of $1 times (see "perldoc perlop", section on
"Multiplicative Operators" for explanation of the x op, see "perldoc
-f length" for an explanation of the length function.

Some example code:
################## begin code
use strict;
use warnings;
while (<DATA> ) {
chomp(my $orig =3D $_);
s/-(-+)/"-".("x"x length$1)/ge;
print "$orig =3D=3D=3D=3D> $_";
}
__DATA__
bla
- Ram
-- bla
bla ---
-- blah ---
################## end code
=20
HTH,
--=20
Offer Kaye
John W. Krahn

2005-04-27, 8:56 pm

Rampra A Padmanabhan wrote:
> Hi all,


Hello,

> I want a regex to replace all continuous occurrences of '-' with
> something else say 'x' except the first one
>
> These are some examples
>
> "- Ram" ===> "- Ram"
> "-- bla" ===> "-x blah"
> "bla ---" ===> "bla -xxx"
>
> And also
> "-- blah ---" ===> "-x blah -xx"


$ perl -le'
my @x = ( "- Ram", "-- bla", "bla ---", "-- blah ---" );
for ( @x ) {
print;
s/(-)(-*)/ $1 . "x" x length $2 /eg;
print;
}
'
- Ram
- Ram
-- bla
-x bla
bla ---
bla -xx
-- blah ---
-x blah -xx



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com