Regex不可打印字节
Regex non printable bytes
如何与非可打印的Unicode或ASCII字节匹配正则表达式?
char cData[1024] = { 0 };
memcpy(cData, "x00x04x02x08x00hello 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);
指出:
您需要在
[ - ]
范围内插入文字字符{
需要在基本正则表达式中进行转义(.?)*
与.*
效果相同。你必须使用这个构造函数,它需要字符串的长度作为另一个参数,因为
x00
空字符将结束字符串。
这就是解决方案。我使用了错误的'regex_constants'。
谢谢Mike的帮助。但是,经过大量的调试尝试,我发现其中一个工作得很好!
#include <iostream>
#include <string>
#include <regex>
int main()
{
char cData[1024] = "x00x04x02x08x01Haaaaa";// { 0 };
char cReg[] = "([ -x1F]{5})(.*)";
int aux[sizeof(cReg)];
for (int i = 0; i < sizeof(cReg); i++)
{
aux[i] = cReg[i];
}
std::match_results<char*> mc;
std::initializer_list<int> list(aux, aux + 14);
std::regex r2e(cReg,14, std::regex_constants::ECMAScript);
if (std::regex_match((char*)cData, cData+10, mc, r2e, std::regex_constants::match_default)) {
for (auto it : mc)
std::cout << it.str().c_str() << std::endl;
}
else {
std::cout << "NOK" << std::endl;
}
std::string name;
std::cin >> name;
}
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- 将字节打印为长字节
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 在 Visual Studio 调试器中,如何从内存中打印字节范围?
- C/C++ 以十六进制打印字节,得到奇怪的十六进制值
- 在C中垂直打印字节数组
- 如何将二进制字节转换为可打印的数值
- 将 MFRC522 UID 十六进制字节转换为可打印的小数
- 如何在文件中打印位而不是字节
- 手动打印 N 字节整数
- 如何在 4 字节整数中打印出每个字节的内容
- 如何将字节打印为十六进制
- 一次打印 2 个字节的十六进制数字
- 在Qt中打印8字节整数时出错
- 将整数打印为以小端序排列的4个字节的集合
- 以十六进制或十进制形式将std::字符串作为字节序列打印到stdout
- 打印字节的正确类型转换
- 打印字节序列
- Regex不可打印字节