Click here to get back home

how to substitute text - special case

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
how to substitute text - special case pauls 02-20-2008
Get Chitika Premium
Posted by pauls on February 20, 2008, 5:54 pm
Please log in for more thread options
I have a text file in which I need to replace a plus sign surrounded by
spaces with just a single plus sign:

this:

ua = 1.5745e-09 + (3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2

becomes:
ua = 1.5745e-09+(3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2

I tried this:
s/ + /+/g;

But it did not seem to work...


Thanks, all help appreciated!!

P

Posted by A. Sinan Unur on February 20, 2008, 6:06 pm
Please log in for more thread options
YcgMiHanZ2dnUVZ_sytnZ2d@seanet.com:

> I have a text file in which I need to replace a plus sign surrounded
by
> spaces with just a single plus sign:
>
> this:
>
> ua = 1.5745e-09 + (3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2
>
> becomes:
> ua = 1.5745e-09+(3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2
>
> I tried this:
> s/ + /+/g;
>
> But it did not seem to work...

Well, I would venture to guess that it worked perfectly fine. The fact
that perl could not read your mind and change the meaning of characters
used in a regex to match your imagination does not imply that it did not
work.

Consulting the docs first is a far more productive way of solving
problems.

C:\> perldoc perlre

...

Quantifiers
The following standard quantifiers are recognized:
...
+ Match 1 or more times
...

So, the regex above matches one or more spaces followed by a space and
replaces such sequences with the plus sign.

C:\Temp> cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

my $x = ' ' x 9 . ' - ' . ' ' x 1024;
$x =~ s/ + /+/g;

print "$x = $x\n";

C:\Temp> t
$x = +-+

It works! For even more fun, try

$x =~ s/ +? /+/g;

...

Now, to achieve what you want:

C:\Temp> cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

my $x = q{1.5745e-09 + (3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2};
$x =~ s/ \+ /+/g;

print "$x = $x\n";

C:\Temp> t
$x = 1.5745e-09+(3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2


--
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.rehabitation.com/clpmisc.shtml>


Posted by John Bokma on February 20, 2008, 6:07 pm
Please log in for more thread options

> I have a text file in which I need to replace a plus sign surrounded by
> spaces with just a single plus sign:
>
> this:
>
> ua = 1.5745e-09 + (3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2
>
> becomes:
> ua = 1.5745e-09+(3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2
>
> I tried this:
> s/ + /+/g;
>
> But it did not seem to work...

s/ \+ /+/g;

+ is a quantifier (1 or more) on the left side of the s///.

--
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1.html

Posted by lars on February 20, 2008, 6:07 pm
Please log in for more thread options
pauls schrieb:
> I have a text file in which I need to replace a plus sign surrounded by
> spaces with just a single plus sign:
>
> this:
>
> ua = 1.5745e-09 + (3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2
>
> becomes:
> ua = 1.5745e-09+(3.3585e-09)*((temp+273.15)/(27+273.15)-1)**2
>
> I tried this:
> s/ + /+/g;
>
> But it did not seem to work...
>
>
> Thanks, all help appreciated!!
>
> P

Try it this way

s/ \+ /\+/g

Posted by Gunnar Hjalmarsson on February 20, 2008, 6:23 pm
Please log in for more thread options
lars wrote:
> Try it this way
>
> s/ \+ /\+/g
---------^
What's the reason for that backslash?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Similar ThreadsPosted
FAQ: How do I substitute case insensitively on the LHS while preserving case on the RHS? October 17, 2004, 11:10 am
FAQ: How do I substitute case insensitively on the LHS while preserving case on the RHS? October 17, 2004, 5:10 pm
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? February 25, 2005, 12:03 pm
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? April 9, 2005, 11:03 am
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? June 25, 2005, 5:03 pm
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? October 20, 2005, 4:03 pm
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? December 13, 2005, 5:03 am
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? January 6, 2006, 11:03 am
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? January 13, 2006, 11:03 am
FAQ 6.5 How do I substitute case insensitively on the LHS while preserving case on the RHS? May 3, 2006, 3:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap