4

What is the regex to match any Chinese character in R?

[\\p{Han}] doesn't seem to work as expected.

v=c("a","b","c","中","e","文")
grep("[\\p{Han}]",v, value = TRUE)

[1] "a"
1
  • 1
    Try this rudimentary option: Filter(function(x) Encoding(x)=="UTF-8", v)
    – NelsonGon
    Jun 23, 2019 at 11:02

1 Answer 1

3

According to regular-expressions.info, "The JGsoft engine, Perl, PCRE, PHP, Ruby 1.9, Delphi, and XRegExp can match Unicode scripts". So setting perl = T should produce the correct results. The R default is a modified version of Ville Laurikari's TRE engine (source):

grep("[\\p{Han}]", v, value = T, perl = T)

#### OUTPUT ####

[1] "中" "文"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.