使用RegExp匹配Unicode字母 [英] Matching Unicode letters with RegExp
问题描述
我需要匹配Unicode字母,类似于PCRE的 \p {L}
。
I am in need of matching Unicode letters, similarly to PCRE's \p{L}
.
现在,由于Dart的RegExp类基于ECMAScript的类,因此遗憾的是,它没有 \p {L}
的概念。
Now, since Dart's RegExp class is based on ECMAScript's, it doesn't have the concept of \p{L}
, sadly.
我正在考虑构造一个可以匹配所有Unicode字母的大字符类,但是我不确定从哪里开始。
I'm looking into perhaps constructing a big character class that matches all Unicode letters, but I'm not sure where to start.
我想匹配字母,例如:
foobar
מכון ראות
但不应匹配R符号:
BlackBerry®
任何ASCII控制字符或标点符号等都不应该基本上,每种Unicode语言都支持的字母,无论是å,ä,φ还是ת,如果它们是实际字母,都应该匹配。
Neither should any ASCII control characters or punctuation marks, etc. Essentially every letter in every language Unicode supports, whether it's å, ä, φ or ת, they should match if they are actual letters.
推荐答案
我知道这是一个老问题。但是 RegExp
现在支持 unicode类别(从Dart 2.4开始),因此您可以执行以下操作:
I know this is an old question. But RegExp
now supports unicode categories (since Dart 2.4) so you can do something like this:
RegExp alpha = RegExp(r'\p{Letter}', unicode: true);
print(alpha.hasMatch("f")); // true
print(alpha.hasMatch("ת")); // true
print(alpha.hasMatch("®")); // false
这篇关于使用RegExp匹配Unicode字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!