1

How to match Chinese characters in Perl? Why

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

doesn't work?

qazwsx
  • 23,658
  • 28
  • 68
  • 99

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
  • 343,984
  • 15
  • 249
  • 495