Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

CR's and other control chars

by Anonymous Monk
on Apr 16, 2009 at 05:01 UTC ( #757841=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm new to perl and I'm trying to modify some perl code that processes text going out of an SMS gateway. Currently it suppresses all control characters using this line:
$msg =~ s/[[:cntrl:]]+/ /sg; #(remove CR LF, etc. ORIGINAL LINE)
I want to allow newlines thru but still suppress the other control chars in case they cause trouble with the SMS. Thinking of trying something like:
$msg =~ s/[\n\r]/[~^]/sg; $msg =~ s/[[:cntrl:]]+/ /sg; #(remove CR LF, etc. ORIGINAL LINE) $msg =~ s/[~^]/[\n\r]/sg;
Is there a cleaner way to do it? Would it be better to use tr rather than s ? Can I exclude ranges of chars rather than substituting?

Replies are listed 'Best First'.
Re: CR's and other control chars
by ikegami (Patriarch) on Apr 16, 2009 at 05:06 UTC

    [ PerlMonks Discussion is for discussions about PerlMonks. I moved your post to Seekers of Perl Wisdom ]

    /[[:cntrl:]]/ is equivalent to /[^[:^cntrl:]]/, so

    # Replace everything except non-controls, CR and LF. $msg =~ s/[^[:^cntrl:]\r\n]+/ /g;

    The "s" modifier is useless since "." isn't being used.

      Thank you.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://757841]
Approved by ikegami
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2023-12-11 06:37 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.