qq_41822833 2023-03-29 16:12 采纳率: 60%
浏览 28
已结题

使用正则表达式查找字符串错误

不能理解为何这样一个这个的代码就会报函数已删除的错误

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    regex r;
    try {
        r.assign("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase);
    }catch(regex_error e)
    {
        cout << e.what() << "\ncode: " << e.code() << endl;
    }
    smatch results;
    if (regex_search(string("myfile.cc"), results, r))
        cout << results.str() << endl;
}

img


但是下面这样的代码就可以正常查找,有没有人知道原因为何?

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    regex r;
    try {
        r.assign("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase);
    }catch(regex_error e)
    {
        cout << e.what() << "\ncode: " << e.code() << endl;
    }
    smatch results;
    string s = "myfile.cc";
    if (regex_search(s, results, r))
        cout << results.str() << endl;
}
  • 写回答

3条回答 默认 最新

  • 关注

    string参数为右值的regex_search()函数在声明时被标为delete了. 编译器的设置.

    #if _LIBCPP_STD_VER > 11
    template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
    bool
    regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
                 match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
                 const basic_regex<_Cp, _Tp>& __e,
                 regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
    #endif
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 已采纳回答 3月29日
  • 创建了问题 3月29日