Code Comments
Programming Forum and web based access to our favorite programming groups.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 ----------------------------------------------------------
Post Follow-up to this messageRampraA 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.]
Post Follow-up to this messageOn 4/27/05, RampraA 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
Post Follow-up to this messageRampraA 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.