【问题标题】:TR1 regex_replace with wstring in VS2010?TR1 regex_replace 在 VS2010 中用 wstring 替换?
【发布时间】:2011-05-21 14:26:37
【问题描述】:
#include <iostream>
#include <string>
#include <regex>
#include <ios>
#include <locale>

using namespace std;

int main () 
{

const wstring wstr(L"<(.|\\n)*?>");
static const wregex wr(wstr);
wstring line (L"<tag>Random text<tag>"); 
wstring line2 (L""); 
wcout << regex_replace<wchar_t>(line,wr,line2) << endl;

}

编译器说:

ClCompile:
  html.cpp
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
          c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
          c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to  typedef of "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
          c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to  typedef "std::tr1::regex_replace"

【问题讨论】:

  • 抱歉,请问有什么问题?
  • sry,我试图编译它,但它从“const std::tr1::wregex”返回有关模板“const std::tr1::basic_regex<_elem> &”的错误,
  • 所以问题是 - 如何使用这些字符串使其工作并正确显示结果,我尝试了 regex_replace(line,wr,line2),没有 - 它返回一些随机信息,例如“00423DA4”
  • 你能复制错误吗?只是说一些笼统的事情不会帮助任何人帮助你。
  • 已将 em 添加到问题中

标签: c++ regex tr1


【解决方案1】:

部分答案:

你应该这样使用regex_repalce

wcout << regex_replace(line,wr,line2) << endl;

即,没有wchar_t。第一个参数用于 Element Traits 类,您很少需要修改它。

编辑

我已经使用 VC++ 2010 检查了您的代码。按照我指定的更改行允许代码编译,并按预期返回结果。可以再试一次吗?

【讨论】:

  • 是的,我发现函数中的语言环境出现了一些语法错误,我习惯将 line1 填满,删除 解决了问题。
猜你喜欢
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 2021-02-14
  • 2011-08-22
  • 2019-12-30
  • 1970-01-01
相关资源
最近更新 更多