Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replace spaces

Reply
Thread Tools

replace spaces

 
 
mud_saisem
Guest
Posts: n/a
 
      08-03-2009
Hi There,

Could somebody please let me know what the regular expression should
look like if I wanted the only replace the spaces of words between "
".

Example:
string: 1,2,3,4 "5 6 7 8" 9,10,11 "12 13" 14,15,16
result: 1,2,3,4 5_6_7_8 9,10,11 12_13 14,15,16
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-03-2009
On Mon, 3 Aug 2009 15:28:20 -0700 (PDT), mud_saisem <(E-Mail Removed)> wrote:

>Hi There,
>
>Could somebody please let me know what the regular expression should
>look like if I wanted the only replace the spaces of words between "
>".
>
>Example:
>string: 1,2,3,4 "5 6 7 8" 9,10,11 "12 13" 14,15,16
>result: 1,2,3,4 5_6_7_8 9,10,11 12_13 14,15,16


s/"(.*?)"/$1/g

If you have something else in mind for $1
do it somewhere else s//&subcall/ge

-sln

 
Reply With Quote
 
 
 
 
mud_saisem
Guest
Posts: n/a
 
      08-04-2009
On Aug 4, 9:42*am, Big and Blue <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > If you have something else in mind for $1
> > do it somewhere else s//&subcall/ge

>
> Well - he asked for replacing spaces with underscores, so:
>
> =====
> my $str = q|1,2,3,4 "5 6 7 8" 9,10,11 "12 13" 14,15,16|;
>
> sub unspace {
> * * *my $x = shift;
> * * *$x =~ tr/ /_/;
> * * *return $x;}
>
> $str =~ s/"(.*?)"/unspace "$1"/ge;
>
> print $str, "\n";
>
> --
> * * * * * * * Just because I've written it doesn't mean that
> * * * * * * * * * *either you or I have to believe it..


I like your work !, I will make very good use of the example that you
have supplied. Thanks for all the effort.

I was trying the following which seemed to be on the right track but
not quite there.

's/"[^*"]*"/_/g'

which was resulting in 1,2,3,4 _ 9,10,11 _ 14,15,16

I just could not figure out how to replace the /_/g with /$1/g but
with the spaces replaced with underscores. If that makes any sense.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments