Perl - need help removing special characters

Discussion in 'Programming' started by Christian Little, Oct 5, 2009.

  1. #1
    How do I get rid of that chunk of special characters?
     
    Christian Little, Oct 5, 2009 IP
  2. ohteddy

    ohteddy Member

    Messages:
    128
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #2
    You can use a regular expression to remove everything but alphanumeric and punctuation characters.

    
    $string =~ s/[^[:alnum:]^[:punct:]]+//;
    
    Code (markup):
    I have not verified this regex works, but you can use a site like http://regex.powertoy.org/ to verify it before you use it.
     
    ohteddy, Oct 5, 2009 IP
    Christian Little likes this.
  3. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but I managed to find a regex that worked. There were people having the same problem as me and they used a regex to wipe out alphanumerics > 127, which is all special text characters:

    $test_string =~ s/(.)/(ord($1) > 127) ? "" : $1/egs;
    Code (markup):
    BEFORE:
    AFTER:


    If I run into more bugs with the code I'll try what you suggested to see if it helps.
     
    Christian Little, Oct 6, 2009 IP