为什么与“cmatch"一起工作的 C++ 正则表达式代码使用“smatch"引发异常? [英] Why do a C++ regular expression code that works with "cmatch" raises an exception with "smatch"?

查看:70
本文介绍了为什么与“cmatch"一起工作的 C++ 正则表达式代码使用“smatch"引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C++ 正则表达式的新手,无法让它们使用字符串而不是 char*.到目前为止,我看到的示例总是针对 c 字符串.

I am new to C++ regular expressions and cannot make them work with strings instead of char*. The examples I have seen so far were always for c strings.

我的真实程序,我什至不会在这里展示,使用子匹配,但我不能让它们工作,所以我试图修改一个非常简单的工作示例,但它也不起作用.我使用 Visual Studio 2010 Ultimate.

My real program, which I will not even try to show here, uses sub matches but I could not make them work, so I tried to modify a very simple working example but it does not work either. I use Visual Studio 2010 Ultimate.

原始 - 工作 - 代码:

The original - working - code:

const char *first = "abcd"; 
const char *last = first + strlen(first); 
std::cmatch mr; 
std::regex rx("abc"); 
std::regex_constants::match_flag_type fl = std::regex_constants::match_default;

std::cout << "search(f, l, \"abc\") == " << std::boolalpha 
          << regex_search(first, last, mr, rx) << std::endl; 
std::cout << "  matched: \"" << mr.str() << "\"" << std::endl; 

std::cout << "search(\"xabcd\", \"abc\") == " << std::boolalpha
          << regex_search("xabcd", mr, rx) << std::endl; 
std::cout << "  matched: \"" << mr.str() << "\"" << std::endl;

修改后的代码:

const string first = "abcd";     // char * => string
std::smatch mr;                  // cmatch => smatch
std::regex rx(string("abc")); 
std::regex_constants::match_flag_type fl = std::regex_constants::match_default;

               // this works:
std::cout << "search(f, l, \"abc\") == " << std::boolalpha 
          << regex_search(first, mr, rx) << std::endl; 
std::cout << "  matched: \"" << mr.str() << "\"" << std::endl; 

               // after the next line executes mr seems good to me:
               // mr[0] = {3, matched:true, first="abcd", second="d",...}
std::cout << "search(\"xabcd\", \"abc\") == " << std::boolalpha
          << regex_search(string("xabcd"), mr, rx) << std::endl; 
               // but the following line gives the error
               // "Debug assertion failed"
               // Expression: string iterators incompatible
std::cout << "  matched: \"" << mr.str() << "\"" << std::endl;

奇怪的是,修改后的代码的一部分有效,而下一部分导致异常.我什至尝试使用 mr[0].str() 但我得到了同样的错误信息.你能帮我解决这个问题吗?

Strange that one part of the modified code works, while the next part causes an exception. I even tried to use mr[0].str() but I got the same error message. Could you help me to solve this problem?

推荐答案

该问题属于临时问题之一.

The issue is one of temporaries.

smatch 将包含您要搜索的字符串的迭代器.

smatch will contain iterators into the string you are searching for.

regex_search(string("xabcd"), mr, rx) 创建一个临时字符串,该字符串在 ;消亡.

regex_search(string("xabcd"), mr, rx) creates a temporary string that dies at the ;.

因此,当您在下一行使用 mr 时,它指的是无效内存.string 应该比 mr 存活的时间更长.

Therefore by the time you use mr at the next line, it refers to invalidated memory. The string should live longer than mr.

这篇关于为什么与“cmatch"一起工作的 C++ 正则表达式代码使用“smatch"引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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