replace spaces

Discussion in 'Perl Misc' started by mud_saisem, Aug 3, 2009.

  1. mud_saisem

    mud_saisem Guest

    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
     
    mud_saisem, Aug 3, 2009
    #1
    1. Advertising

  2. mud_saisem

    Guest

    On Mon, 3 Aug 2009 15:28:20 -0700 (PDT), mud_saisem <> 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
     
    , Aug 4, 2009
    #2
    1. Advertising

  3. mud_saisem

    mud_saisem Guest

    On Aug 4, 9:42 am, Big and Blue <> wrote:
    > 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.
     
    mud_saisem, Aug 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John B. Matthews
    Replies:
    4
    Views:
    667
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    625
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,600
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    876
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,899
    Mayeul
    Dec 10, 2009
Loading...

Share This Page