无法检索正则表达式匹配结果 - MFC/C++ [英] Unable to retrieve regex match results - MFC / C++

查看:87
本文介绍了无法检索正则表达式匹配结果 - MFC/C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一个 HTML 页面并尝试检索其中的特定字符串.

I am reading an HTML page and trying to retrieve a specific string within it.

我有以下代码:

    std::string str = test.GetString(); // someString ( the string i have checked it, it's basically an html page )
    std::smatch match;
    std::regex re("0x(\\d|[A-Z]).*0000"); // the pattern I'm searching for
    if (std::regex_search(str, match, re)){
        test = "found"; // found gets printed
    }
    TRACE("%s\n",match[0]); // this outputs some garbage like this '˜ò'

我想打印/存储找到的匹配结果,但我得到了一些垃圾.

I want to print/store the result of the match found but I get some garbage instead.

免责声明:我是 C++ 正则表达式的新手.我可能犯了一个基本错误

Disclaimer: I'm new to C++ regex. I might be doing a basic mistake

推荐答案

std::smatch match;
...
TRACE("%s\n",match[0]); // this outputs some garbage like this '˜ò'

TRACE 宏中的 %s 类型说明符需要一个 原始 C 字符串指针(char* 在 ANSI/MBCS 构建中;wchar_t* 在 Unicode 构建中 - 我假设您正在此处进行 ANSI/MBCS 构建.).

The %s type specifier in the TRACE macro expects a raw C string pointer (char* in ANSI/MBCS builds; wchar_t* in Unicode builds - I'm assuming you are doing an ANSI/MBCS build here.).

但是 match[0] 不是一个原始的 C 字符串指针.

But match[0] is not a raw C string pointer.

因此,您通过 %s(即原始 C 字符串 char* 指针)向 TRACE 承诺的内容不匹配,并且你实际上传递给它的是什么(即match[0]).

So you have a mismatch between what you promised to TRACE via %s (i.e. a raw C string char* pointer), and what you are actually passing to it (i.e. match[0]).

根据一些在线文档std::smatchstd::match_results 模板的特化,特别是:

According to some online documentation, std::smatch is a specialization of the std::match_results template, in particular:

smatch --> match_results<string::const_iterator>

smatch::operator[](您在代码中将其作为 match[0] 调用)返回对另一个对象的引用,它是一个std::sub_match.这个 std::sub_match 代表一对迭代器,表示匹配字符的序列.

smatch::operator[] (which you are invoking in your code as match[0]) returns a reference to another object, which is a std::sub_match. This std::sub_match class represents a pair of iterators, denoting sequences of matched characters.

因此,您承诺TRACE 传递原始 C 字符串指针(通过 %s 类型说明符),但您是实际上传递了一个完全不同的东西,即对 std::sub_match 对象的引用(通过你的 match[0] 代码):难怪印刷的文字毫无意义.

So, you are promising to TRACE to pass a raw C string pointer (via the %s type specifier), but you are actually passing a completely different thing, i.e. a reference to a std::sub_match object (via your match[0] code): no wonder that the printed text is meaningless.

你需要做的是从 match[0] 表达式中获取一个 C 字符串指针.

What you have to do is to obtain a C string pointer from the match[0] expression.

为此,您可以调用 std::sub_matchstr() 方法.这将返回一个 std::string 对象.

To do that, you can invoke the std::sub_match's str() method. This returns a std::string object.

然而,这个 std::string 对象正是 %s 所期望的:事实上,%s 代表一个原始的 C 字符串指针(例如 const char*),不是一个 std::string 实例.

However, this std::string object is not exactly what %s expects: in fact, %s represents a raw C string pointer (e.g. const char*), not a std::string instance.

所以,最后一步是从 std::string 对象中提取这个原始的 C 字符串指针,这是通过调用 std::string::c_str() 方法.

So, the last step is to extract this raw C string pointer from the std::string object, and this is done by invoking the std::string::c_str() method.

总结这些逻辑步骤:

std::smatch match;
...
match[0]               --> reference to std::sub_match object
match[0].str()         --> std::string object
match[0].str().c_str() --> raw C string pointer (const char*)

所以,你的 TRACE 语句可以写成:

So, your TRACE statement can be written as:

TRACE("%s\n", match[0].str().c_str());

这篇关于无法检索正则表达式匹配结果 - MFC/C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆