Xu Wang
Xu Wang

Reputation: 10607

Catch any control key (e.g. \cn and \cx) in perl

By control key here I mean the control key on the keyboard plus any letter. I can match them individually, by e.g. \cn or \cx, but how can I match all such keys?

Upvotes: 0

Views: 185

Answers (3)

Borodin
Borodin

Reputation: 126742

The POSIX character class /[[:cntrl:]]/ is what you want.

It matches character codes 0 .. 0x1F and DEL (0xFF)

Upvotes: 1

Steve Sanbeg
Steve Sanbeg

Reputation: 887

You can match against a character class, i.e.

perl -lne 'print m/[[:cntrl:]]/?"yes":"no"'

Upvotes: 2

mob
mob

Reputation: 118665

Do you mean match in a regular expression? You can use a ranged character class:

$contains_ctrl_char = $_ =~ /[\c@-\c_]/;
$contains_ctrl_char = $_ =~ /[\000-\037]/;
$contains_ctrl_char = $_ =~ /[\x{00}-\x{1F}]/;

There's probably an appropriate POSIX character class for this, too, though I don't know what it is off hand.

Upvotes: 2

Related Questions