qazwsx
qazwsx

Reputation: 26898

How to match Chinese characters in Perl?

How to match Chinese characters in Perl? Why

$ perl -e 'if ( "中国" =~ /\p{Han}/ ) { print "!"}'
$

doesn't work?

Upvotes: 1

Views: 2077

Answers (1)

ikegami
ikegami

Reputation: 385829

If your source code is UTF-8, you need to use use utf8;. If it isn't UTF-8, the source couldn't possibly have any Han characters in it.

$ perl -le'use utf8; if ( "中国" =~ /\p{Han}/ ) { print "!" }'
!

Upvotes: 10

Related Questions