Remove Unicode control character from string

R

Ryan Chan

Hello,

Consider the sample code:
##############
use strict;

my $in = "\U0004";
my $out = chr($in) . "apple";

$out =~ s/[:cntrl:]//gi;
print $out;
##############

I want to remove the the first unicode char (e.g. "\U0004") from the
string, I found the above code does not work as expected, any idea?

Thanks.
 
B

Ben Bullock

Hello,

Consider the sample code:
##############
use strict;

my $in = "\U0004";
my $out = chr($in) . "apple";

$out =~ s/[:cntrl:]//gi;
print $out;
##############

I want to remove the the first unicode char (e.g. "\U0004") from the
string, I found the above code does not work as expected, any idea?

I got a message like this:

POSIX syntax [: :] belongs inside character classes in regex; marked
by <-- HERE
in m/[:cntrl:] <-- HERE / at ./moo.pl line 6.

It seems you need more []s.

$out =~ s/[[:cntrl:]]//gi;

seems to do the trick.
 
J

Jürgen Exner

Ryan Chan said:
$out =~ s/[:cntrl:]//gi;

I want to remove the the first unicode char (e.g. "\U0004") from the
string, I found the above code does not work as expected, any idea?

You can use the notation [:cntrl:] only inside of a character class.
From "perldoc perlre":

The POSIX character class syntax

[:class:]

is also available. Note that the "[" and "]" brackets are *literal*;
they must always be used within a character class expression.

# this is correct:
$string =~ /[[:alpha:]]/;

# this is not, and will generate a warning:
$string =~ /[:alpha:]/;

jue
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top