whitespace remove

Discussion in 'Perl' started by abhisheksainiabhishe, Jun 11, 2008.

  1. abhisheksainiabhishe

    abhisheksainiabhishe New Member

    Joined:
    Jun 10, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    If I have a multiline text like..

    abcd ef ghi
    xyz d e f
    d
    sjskf s fdf

    then how would I be able to remove the spaces (/s+) inside the strings (in middle of the strings).

    I thought using doing like...

    $string =~ s/\s+/ /g;

    would work. but it does not work.

    And if above is the way I would end up finally then can I make it like so it would
    not alter spaces within <pre> tags like....

    #declaring pretags avlyes
    my $startPreTag="<pre>";
    my $endPreTag="</pre>";


    #so the spaces (more than one) will be removed is it is not enclosed by pre tags
    $string =~ s/[^$startPreTag]^\s+[^$endPreTag]//;

    Please tell me if I am on the right track

    None of my appraoches are working. Please advise. Thanks!
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    You expression looks like having a space in the replacement portion,
    Code:
    # Your version
    $string =~ s/\s+/ /g;
    # Right code
    $string =~ s/\s+//g;
    
    For replacement within PRE tags,
    Code:
    $string =~ /<pre>([^<]+)<\/pre>/gi;
    $str = $1;
    $str =~ s/\s+//g;
    $string =~ s/<pre>([^<]+)<\/pre>/$str/;
    
    This will work only if there is only one occurance of pre in the string, you can also try HTML::Parse for this purpose.
     
  3. abhisheksainiabhishe

    abhisheksainiabhishe New Member

    Joined:
    Jun 10, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
  4. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    561
    Likes Received:
    19
    Trophy Points:
    0
    DONT SPAM THAT GAY *** GAME called runescape u will pay for spamming those message i swear to god you will. I hate runescape with a passion its the most retardest game ever made U WILL PAY I SWEAR TO GOD ON THAT
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Hi account is banned and all posts removed as well as the site goes into spam sites
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice