Excluding in Substitution

Discussion in 'Perl Misc' started by Grant Nosbush, Nov 15, 2004.

  1. How would I go about removing spaces from a string in perl except
    those spaces found in brackets? In other words, if I have the
    following string:

    $str = "Hello World (My name is Bob) What is your name?"

    I would like it to become:

    $str = "HelloWorld(My name is Bob)Whatisyourname?"

    I was thinking of using substituion in Perl, but not sure what
    parameters to use. Anyone have any ideas? Thanks for your help!
    Grant Nosbush, Nov 15, 2004
    #1
    1. Advertising

  2. Grant Nosbush

    Lukas Mai Guest

    Grant Nosbush schrob:
    > How would I go about removing spaces from a string in perl except
    > those spaces found in brackets? In other words, if I have the
    > following string:

    [...]

    perldoc -q except

    HTH, Lukas
    --
    main(int v,char**c){c?main(atoi(c[--v]),0),
    puts(""):putchar((v/2&&main(v/2,0),48|v&1));}
    Lukas Mai, Nov 15, 2004
    #2
    1. Advertising

  3. Grant Nosbush

    Anno Siegel Guest

    Grant Nosbush <> wrote in comp.lang.perl.misc:
    > How would I go about removing spaces from a string in perl except
    > those spaces found in brackets? In other words, if I have the
    > following string:
    >
    > $str = "Hello World (My name is Bob) What is your name?"
    >
    > I would like it to become:
    >
    > $str = "HelloWorld(My name is Bob)Whatisyourname?"


    Split the string on the parenthesized pieces, capturing the
    delimiters. Go through the partial strings and delete spaces in
    strings that don't begin with "(". Join everything together again.

    $_ = 'Hello World (My name is Bob) What is your name?';
    $str = join '', map { tr/ //d unless /^\(/; $_} split /(\([^)]*\))/;

    Anno
    Anno Siegel, Nov 15, 2004
    #3
  4. Grant Nosbush

    Lukas Mai Guest

    Grant Nosbush schrob:
    > How would I go about removing spaces from a string in perl except
    > those spaces found in brackets? In other words, if I have the
    > following string:


    > $str = "Hello World (My name is Bob) What is your name?"


    > I would like it to become:


    > $str = "HelloWorld(My name is Bob)Whatisyourname?"


    > I was thinking of using substituion in Perl, but not sure what
    > parameters to use. Anyone have any ideas? Thanks for your help!


    1 while $str =~ s/^([^(\s]*(?:\([^)]*\)[^(\s]*)*)\s+/$1/;

    1 while s/
    ^ # beginning of string
    ( # capture
    # this is what we want to keep:
    [^(\s]* # 0 or more non-whitespace non-( chars
    (?: # a group of:
    \( # opening (
    [^)]* # 0 or more non-) chars
    \) # closing )
    [^(\s]* # 0 or more non-whitespace non-( chars
    )* # ... repeated 0 or more times
    ) # end of capture
    \s+ # 1 or more whitespace chars
    # note: this is the first run of whitespace not surrounded by ( )
    /
    $1 # replace all of the above with the first capturing group, which
    # contains only non-whitespace and (...) groups
    /xe;
    --
    print+74.117.115.116,,qq.\c!..not::.her,Perl=>q$hacker,$,!($,=$")
    Lukas Mai, Nov 15, 2004
    #4
    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. Paul Cook
    Replies:
    1
    Views:
    380
    Harley
    Aug 7, 2003
  2. Frank Rizzo
    Replies:
    2
    Views:
    3,028
    Frank Rizzo
    Jan 12, 2004
  3. Jon Maz

    Excluding a file from VS.Net Project

    Jon Maz, Nov 18, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    535
    Jon Maz
    Nov 19, 2004
  4. Olav Tollefsen
    Replies:
    2
    Views:
    346
    Olav Tollefsen
    Feb 1, 2005
  5. Andy Fish

    excluding some rows when databinding

    Andy Fish, Mar 31, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    687
    Brock Allen
    Mar 31, 2005
Loading...

Share This Page