c++ - 正则表达式不可打印字节

标签 c++ regex unicode ascii

如何获得与不可打印的 Unicode 或 ASCII 字节匹配的正则表达式?

    char cData[1024] = { 0 };
    memcpy(cData, "\x00\x04\x02\x08\x00hello thats it", 19);

    std::regex r2e("([\\x00-\\x1F]){5}(.?)*", std::regex_constants::basic);
    if (std::regex_search((char*)cData, cData+19, r2e))
        printf("ok");
    else
        printf("nok");

我的示例不工作(打印“nok”)。

最佳答案

这是解决方案:

std::regex r2e("[\x00-\x1F]\\{5\\}.*", 12, std::regex_constants::basic);

注意事项:

  1. 您需要在[ - ] 范围内插入文字字符。

  2. {需要在基本正则表达式中进行转义。

  3. (.?)*.* 效果相同。

  4. 您必须使用此构造函数,它需要字符串的长度作为另一个参数,因为 \x00 空字符将结束字符串。

关于c++ - 正则表达式不可打印字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38147293/

相关文章:

unicode - U+25BE的 "up-pointing"版本是什么?

c++ - 在 Windows 控制台应用程序中输出 unicode 字符串

C++ 函数式 : bind classes method through pointer

c++ - 通用模板函数总是返回整数值

c++ - 从静态变量的析构函数调用的 Windows 7 上的 std::condition_variable notify() 问题

具有不同格式的 Java Regex 数字提取

mysql - LIKE '[charlist]%' 语法在 MySQL 中不起作用 (phpMyAdmin)

c++ - 为什么在使用递归 lambda 时出现编译错误?

python - 在Python中为每个正则表达式匹配添加后缀?

python - python 中的哈希 unicode 字符串