#c #regex #tr1
#c #регулярное выражение #tr1
Вопрос:
#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:usersusrdocumentsvisual studio 2010projectshtmlhtmlhtml.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> amp;,const std::tr1::basic_regex<_Elem,_RxTraits> amp;,const std::basic_string<_Elem> amp;,std::tr1::regex_constants::match_flag_type): not able to output argument template "const std::tr1::basic_regex<_Elem,wchar_t> amp;" from "const std::tr1::wregex"
c:program filesmicrosoft visual studio 10.0vcincluderegex(2739): look to typedef "std::tr1::regex_replace"
c:usersusrdocumentsvisual studio 2010projectshtmlhtmlhtml.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> amp;,const std::tr1::basic_regex<_Elem,_RxTraits> amp;,const std::basic_string<_Elem> amp;,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> amp;" from "const std::tr1::wregex"
c:program filesmicrosoft visual studio 10.0vcincluderegex(2739): look to typedef of "std::tr1::regex_replace"
c:usersusrdocumentsvisual studio 2010projectshtmlhtmlhtml.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> amp;,const std::tr1::basic_regex<_Elem,_RxTraits> amp;,const std::basic_string<_Elem> amp;,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> amp;" from "const std::tr1::wregex"
c:program filesmicrosoft visual studio 10.0vcincluderegex(2739): look to typedef "std::tr1::regex_replace"
Комментарии:
1. Извините, но в чем вопрос?
2. извините, я пытался его скомпилировать, но он возвращает ошибки о шаблоне «const std::tr1::basic_regex<_Elem,wchar_t> amp;» из «const std::tr1::wregex»,
3. итак, вопрос в том, как заставить это работать с этими строками и правильно отображать результаты, я попробовал regex_replace (строка,wr, line2), без <wchar_t> — он возвращает некоторую случайную информацию, такую как «00423DA4»
4. Можете ли вы скопировать ошибку? Просто сказать что-то общее об этом никому не поможет помочь вам.
5. добавили их к вопросу
Ответ №1:
Частичный ответ:
Вы должны использовать regex_repalce
этот способ:
wcout << regex_replace(line,wr,line2) << endl;
т.е. без wchar_t
. Первый аргумент относится к классу Element Traits, который вы лишь в редких случаях захотите изменять.
Редактировать
Я проверил ваш код с помощью VC 2010. Изменение строки, как я указал, позволило коду скомпилироваться и вернуть ожидаемый результат. Можете ли вы попробовать это еще раз?
Комментарии:
1. Да, я обнаружил, что были допущены некоторые синтаксические ошибки с locale в функции, которую я использовал для заполнения line1, удаление <wchar_t> устранило проблему.