POSIX regex.h提供unicode字符还是基本上是非ascii字符?

Does POSIX regex.h provide unicode or basically non-ascii characters?

本文关键字:字符 基本上 是非 ascii 提供 unicode POSIX regex      更新时间:2023-10-16

嗨,我正在使用标准正则表达式库(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(&reg, argv[1], 0)) != 0) {
    char buf[256];
    regerror(ret, &reg, buf, sizeof(buf));
    fprintf(stderr, "regcomp() error (%d): %sn", ret, buf);
    return 1;
  }
  if ((ret = regexec(&reg, 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当然)。