1

How to match Chinese characters in Perl? Why

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

doesn't work?

qazwsx
  • 25,536
  • 30
  • 72
  • 106

1 Answers1

10

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 "!" }'
!
ikegami
  • 367,544
  • 15
  • 269
  • 518