Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

regex substitution
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
----------------------------------------------------------

Report this thread to moderator Post Follow-up to this message
Old Post
Ramprasad A Padmanabhan
04-27-05 01:56 PM


Re: regex substitution
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.]



Report this thread to moderator Post Follow-up to this message
Old Post
Ing. Branislav Gerzo
04-27-05 01:56 PM


Re: regex substitution
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

Report this thread to moderator Post Follow-up to this message
Old Post
Offer Kaye
04-27-05 01:56 PM


Re: regex substitution
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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-28-05 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:32 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.