【发布时间】:2016-10-02 15:24:18
【问题描述】:
#include <boost/regex.hpp>
#include <string>
#include <vector>
#include <iostream>
int main(int argc, char* argv[]) {
std::string text = argv[1];
std::string patterns = argv[2];
boost::regex regex = boost::regex(patterns);
boost::smatch match;
std::cout << boost::regex_search(text, match, regex) << std::endl;
}
如果我在输入 hello¿ ¿(包含一个非 ASCII 字符和 UTF-8 编码)上运行程序,它会返回 0 即未找到,但如果我在输入上运行它 hel√ √(再次包含非ASCII)它返回1,即找到。
我的问题:boost::regex(即 ascii 版本)在运行 utf 字符时的预期行为是什么?
编辑:感谢所有 cmets,我仍然对为什么输出 1 很感兴趣,因为文本和正则表达式都包含非 ascii 字符。我的猜测是字节被解释为 ascii,因此它们匹配。
【问题讨论】:
-
你的
patterns是什么? -
@Wiktor Stribiżew 第二个参数,即¿ √
-
这两个都在我的系统上打印 1,这当然是我所期望的。 (我使用 UTF-8 语言环境,因此字符应该逐字传递给程序。)
-
感谢所有 cmets,我仍然对为什么输出 1 很感兴趣,因为文本和正则表达式都包含非 ascii 字符。我的猜测是字节被解释为 ascii,因此它们匹配......
-
@user695652 你猜对了。例如
√的UTF8编码为E2 88 9A,在Latin-1中被解释为√。这里有趣的问题是为什么你得到一个¿的0,因为它是C2 BF,解释为¿。你能分享一些关于你的系统的细节,以及你是如何调用命令的(即你是如何准确传递参数的)?