简体   繁体   English

在VS2010中使用wstring的TR1 regex_replace?

[英]TR1 regex_replace with wstring in VS2010?

#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;

}

Compiler says: 编译说:

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"

Partial answer: 部分答案:

You should use regex_repalce this way: 你应该这样使用regex_repalce

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

ie, without the wchar_t . 即没有wchar_t The first argument is for the Element Traits class, which you'd only rarely want to modify. 第一个参数是Element Traits类,您很少想要修改它。

Edit 编辑

I've checked your code with VC++ 2010. Changing the line as I specified allowed the code to compile, and return the result as expected. 我已经用VC ++ 2010检查了你的代码。按照我的指定更改行允许代码编译,并按预期返回结果。 Can you try it again? 你能再试一次吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM