POSIX regex.h提供unicode字符还是基本上是非ascii字符?
Does POSIX regex.h provide unicode or basically non-ascii characters?
嗨,我正在使用标准正则表达式库(regcomp, regexec..)。但是现在根据需要,我应该为正则表达式的代码添加unicode支持。
标准正则表达式库提供unicode或基本上非ascii字符?我在网上查了一下,不这么认为。
我的项目是资源评论家,因此我不想为它使用大型库(ICU和Boost.Regex)。
看起来像POSIX Regex在UTF-8语言环境下正常工作。我刚刚写了一个简单的测试(见下文),并使用它来匹配字符串与一个西里尔字符对正则表达式"[[:alpha:]]"
(例如)。一切都很好。
注意:您必须记住的主要事情- regex函数是与语言环境相关的。所以你必须在它之前调用setlocale()
。
#include <sys/types.h>
#include <string.h>
#include <regex.h>
#include <stdio.h>
#include <locale.h>
int main(int argc, char** argv) {
int ret;
regex_t reg;
regmatch_t matches[10];
if (argc != 3) {
fprintf(stderr, "Usage: %s regex stringn", argv[0]);
return 1;
}
setlocale(LC_ALL, ""); /* Use system locale instead of default "C" */
if ((ret = regcomp(®, argv[1], 0)) != 0) {
char buf[256];
regerror(ret, ®, buf, sizeof(buf));
fprintf(stderr, "regcomp() error (%d): %sn", ret, buf);
return 1;
}
if ((ret = regexec(®, argv[2], 10, matches, 0)) == 0) {
int i;
char buf[256];
int size;
for (i = 0; i < sizeof(matches) / sizeof(regmatch_t); i++) {
if (matches[i].rm_so == -1) break;
size = matches[i].rm_eo - matches[i].rm_so;
if (size >= sizeof(buf)) {
fprintf(stderr, "match (%d-%d) is too long (%d)n",
matches[i].rm_so, matches[i].rm_eo, size);
continue;
}
buf[size] = ' ';
printf("%d: %d-%d: '%s'n", i, matches[i].rm_so, matches[i].rm_eo,
strncpy(buf, argv[2] + matches[i].rm_so, size));
}
}
return 0;
}
使用例子:
$ locale
LANG=ru_RU.UTF-8
LC_CTYPE="ru_RU.UTF-8"
LC_COLLATE="ru_RU.UTF-8"
... (skip)
LC_ALL=
$ ./reg '[[:alpha:]]' ' 359 фыва'
0: 5-7: 'ф'
$
匹配结果的长度是两个字节,因为在UTF-8中西里尔字母占用了太多。
基本上,POSIX正则表达式不支持Unicode。您可以尝试在Unicode字符上使用它们,但是具有多种编码的字形可能会出现问题,以及Unicode感知库为您处理的其他此类问题。
来自标准,IEEE Std 1003.1-2008:
匹配应基于用于编码字符的位模式,而不是基于字符的图形表示。这意味着,如果字符集包含图形符号的两个或多个编码,或者如果搜索的字符串包含以多个编码集编码的文本,则不会尝试搜索编码符号的任何其他表示。如果需要,用户可以指定包含所需图形符号的所有变体的等价类。
也许libpcre适合你?它比POSIX正则表达式稍重,但我认为它比ICU或Boost轻。
如果你真正的意思是"标准",即std::regex
从c++ 11,然后所有你需要做的是切换到std::wregex
(和std::wstring
当然)。
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- POSIX regex.h提供unicode字符还是基本上是非ascii字符?