2011-01-30 8 views
7

boost::regex_replace'daki biçim işleviyle ilgili sorun yaşıyorum. Bundan bir parametre versiyonunu değil, iki parametreyi arayabilirsiniz:İki parametre biçimi işleviyle ilgili sorunlar boost :: regex_replace

e = "(^|>)([^<>]+)"; 
h_str = regex_replace(h_str, e, repl_quot, boost::match_default); 

repl_quot

std::string const &repl_quot(boost::smatch const &what) { 
    boost::regex e("\""); 
    std::string repl("&#34;"); 
    static std::string; 
    str = regex_replace(what[0].str(), e, repl, boost::match_default); 
    return str; 
} 

yukarıdaki eserleri olarak tanımlanır, ama gerçekten bu statik kullanmak istemiyoruz

std::string const &repl_quot2(boost::smatch const &what, std::string &out) { 
    boost::regex e("\""); 
    std::string repl("&#34;"); 
    out = regex_replace(what[0].str(), e, repl, boost::match_default); 
    return out; 
} 

Ama bu (bir dolambaçlı derleyici hatası) regex_replace kabul etmeyecektir: değişken, bu yüzden ben kabul edilebilir bir iki parametre alternatif sürümü olduğunu düşündüğü şeyi denedik. Ben Boost::Regex belgelerinden aşağıdakilere dayalı iki parametre sürümünü kullanmaya çalışıyorum:

şablon basic_string regex_replace (const & s basic_string, const & e basic_regex, Formatter bicim, match_flag_type bayrakları = match_default);

tip Formatter olmalıdır gerektirir ya ... bir işlev çağrısından yedek dize hesaplar bir, tekli ikili veya üçlü funktor: char_type en bir kabı dönmelidir hangi olmaya (ne) fmt ya yeni OutputIterator pozisyonu geri sonra yeni metni için * yazmak ve her ikisi de yedek metni veya ya fmt (ne dışarı) ya da fmt (dışarı ne , bayrakları) olarak kullandı. Her durumda temsil eden match_results nesnesi eşleşmeyi buldu.

, işte derleyici hata iletisi için tekrarlanan istekleri olmuştur öyle (dikkatli olmak ne isteyebilir):

c: \ boost \ boost \ regex \ v4 \ regex_format.hpp Üye işlevinde `OutputIter boost :: re_detail :: format_functor_container :: operator() (const Eşleşme &, OutputIter, boost :: regex_constants :: match_flag_type, const Özellikleri &) [OutputIter ile = boost :: re_detail :: string_out_iterator, std: : ayırıcı>>, Container = const std :: string & (*) (const güçlendirme :: smt &, std :: string &), Eşleşme = Artırmak :: < __gnu_cxx :: __ normal_iterator std :: ayırıcısı>>, std :: ayırıcısı, std :: ayırıcısı>>>>>, Soylar> regex_traits_wrapper = boost ::>] match_results:

356 c : \ boost \ boost \ regex \ v4 \ match_results.hpp, OutputIterator boost :: match_results :: biçiminden çıktılandı (OutputIterator, Functor, boost :: regex_constants :: match_flag_type, const RegexT &) const [OutputIterator = destekle birlikte :: re_detail :: string_out_iterator, std :: allocator>>, Functor = const std :: dize & (*) (const boost :: smatch &, std :: dize &), RegexT = boost :: basic_regex>>, BidiIterator = __gnu_cxx :: __ normal_iterator, std :: ayırıcı>>, Allocator = std :: ayırıcısı, std :: ayırıcısı>>>>]

60 c: artırmak OutputIterator `den örneği \ takviye \ artırmak \ normal ifade \ v4 \ regex_replace.hpp :: regex_replace (OutputIterator, BidirectionalIterator, BidirectionalIterator const boost :: basic_regex &, Biçimlendirme, güçlendirme :: regex_constants :: match_flag_type) [OutputIter ile ator = boost :: re_detail :: string_out_iterator, std :: ayırıcı>>, BidirectionalIterator = __gnu_cxx :: __ normal_iterator, std :: ayırıcı>>, traits = boost :: regex_traits>, charT = char, Biçemleyici = const std :: string & (*) (const boost :: smatch &, std :: string &)]'

80 c: std :: basic_string, std' örneklenen \ takviye \ artırmak \ normal ifade \ v4 \ regex_replace.hpp :: allocator < _T2>> boost :: regex_replace (const std :: basic_string, std :: ayırıcı < _T2>> &, const desteği :: basic_regex &, Biçimlendirici, destek :: regex_constants :: match_flag_type) [özelliklerle = destekle :: regex_traits>, charT = char, Biçimlendirici = const std :: dize & (*) (const boost :: smatch &, std :: string &)]'

327 ° C Dev-CPP \ Örnekler \ wordrad \ xhtml_open.cpp burada

1064 c örneği \: \ takviye \ artırmak \ normal ifade \ v4 \ regex_format. üye için hpp isteği begin' in ((boost :: re_detail :: format_functor_container, std :: allocator>>, std :: ayırıcı, std :: ayırıcı>>>> yükselt, :: regex_traits_wrapper>>> *) this) -> boost :: re_detail :: format_functor_container, std :: ayırıcı>>, std :: allocator, std :: allocator>>>>>, boost :: regex_traits_wrapper>>> :: func ', sınıf dışı bir türdür const std :: string & (* const) (const güçlendirme :: smil &, standart: dize &) '

1064 c: \ boost \ boost \ regex \ v4 \ regex_format.hpp üye end' in isteği (boost :: re_detail :: format_functor_container, std :: allocator>>, std :: allocator, std :: allocator> >>>>, boost :: regex_traits_wrapper>>> *) this) -> boost :: re_detail :: format_functor_container, std :: ayırıcı>>, std :: ayırıcı, std :: ayırıcı>>>>>, artırın: : regex_traits_wrapper>>> :: fonk 'olmayan sınıf türü olan `const std :: string & (* const) (const boost :: smatch &, std :: string &)'

+0

Aldığınız hatayı postalamalısınız. –

+0

Çok uzun sürüyor. Başlarını veya kuyruklarını yapamayacağımı tahmin ediyorum. Onun şablonu ile ilgili veya bir şey. Sorun, regex-replace'in fmt parametresi için bir imza, şablonlara ya da her neyse, bir imza olarak beklemesidir. Bu hata mesajı kabul edilemez. – Mark

+0

1064 c: \ boost \ boost \ regex \ v4 \ regex_format.hpp üye 'son' isteğinde '' ((boost :: re_detail :: format_functor_container , std :: allocator >>, std :: allocator , std :: ayırıcı >>>> boost :: regex_traits_wrapper >>> *) this) -> boost :: re_detail :: format_fu ... – Mark

cevap

1

Tamam işte nasıl repl_quot2:

struct formatter 
{  
    template<typename Out> 
    Out operator()(boost::smatch const &what, Out out) const { 
    boost::regex e("\"");  
    std::string repl("&#34;"); 
    std::string str 
     = regex_replace(what[0].str(), e, repl, boost::match_default); 
    out = std::copy(str.begin(), str.end(), out); 
    return out; 
    } 

}; 

Sonra regex_replace onu yürütmesini zaman:

e = "(^|>)[^<>]+"; 
    formatter repl_quot2; 
    h_str = regex_replace(h_str, e, repl_quot2, boost::match_default); 

Bu http://boost-sandbox.sourceforge.net/libs/xpressive/doc/html/boost_xpressive/user_s_guide/string_substitutions.html de belgelere gelir.

Şu anda ne yapmam, iki parametre sürümü çağrıldığında bir işleve karşılık bir işleve (bir işleçle() operatörüne) gereksinim duyması, ancak bir parametre sürümü için bir functor gerektirmemesidir (OP'de repl_quot). Her neyse, düz bir işlev olarak çalışmak için yeni iki parçalı versiyonu almamıştım.Ancak asıl sorun, OP'de olduğu gibi std :: string'i yapmaktan ziyade, gösterildiği gibi şablon parametresi olarak geçmek ve dönmek zorunda olduğum out idi. Onun bir OutputIterator olması gerekiyordu-aslında ne olduğunu bilmiyorum.

Bu arada, tüm bu normal ifade, html varlık sürümüyle çift tırnak yerine, \ & # 34; html içindeki herhangi bir metinde bir etiketin parçası değil.

Ayrıca, benim özgün işlev repl_quot yerine istediği nedeni repl_quot statik yerel değişkende dönüş değeri saklamak zorunda olmasıdır. Normal bir yerel değişkeni döndürmek için işe yaramaz, çünkü kullanılmadan önce (ve çökmelere neden olabilecek şekilde) ayrılabilir. repl_quot çalışıyordu - benim statik ile sorun benim iplik güvenli değil ve Boost :: RegEx multithreading olup olmadığını bilmiyordu. Bence bunu multithreading olarak derledim, ama statik var problemlere sebep olmadı. Ancak repl_quot2 ile dönüş değerini bir çıkış parametresine yazarım.