boost 正則表達式 regex


boost 正則表達式 regex

 

環境安裝

如果在引用boost regex出現連接錯誤,但是引用其他的庫卻沒有這個錯誤,這是因為對於boost來說,是免編譯的,但是,正則這個庫 是需要單獨編譯和使用的。簡單的辦法就是 直接將boost庫全部編譯,然后 找到正則的lib,編譯時候引用進去。

代碼example

#include <boost/regex.hpp> #include <iostream> #include <string> #include "TestRe.h" using namespace::boost; using namespace::std; void TestRe::test() { regex re("(https?://www.ttufo.com/.+/.+/.+)(_\\d+)(.html?)"); //string replace("http://www.ttufo.com/($1)/($2)/($3).htm($5)"); //regex re("http://www.ttufo.com/(.+)/(.+)/(.+)(_.+).htm(l?)"); string target("http://www.ttufo.com/ufo/201705/154053_3.html"); cmatch what; if (regex_match(target.c_str(), what, re)) { cout << "match " << what.size() << endl; for (int i = 0; i < what.size(); i++) { cout << "what[" << i << "]: " << what[i] << ", first: " << what[i].first << ", second: " << what[i].second << endl; } } else { cout << "not match " << endl; } } void TestRe::test_replace() { cout << "test replac ----------------" << endl; string s1 = "(<)|(>)|(&)"; // string s2 = "(?1b)(?2e)(?3...)"; string s2 = "(?1$1)(?2$2)(?3...)"; string target("cout << a&b << endl;"); boost::regex reg( s1 ); string s = boost::regex_replace( target, reg, s2, boost::match_default | boost::format_all); cout << s << endl; cmatch what; target = "cout << a&b << endl;"; if (regex_search(target.c_str(), what, reg)) { cout << "match " << what.size() << endl; for (int i = 0; i < what.size(); i++) { cout << "what[" << i << "]: " << what[i] << ", first: " << what[i].first << ", second: " << what[i].second << endl; } } else { cout << "not match " << endl; } cout << "test replac ----------------" << endl; } void TestRe::test_replace_1() { regex reg("(https?://www.ttufo.com/.+/.+/.+)(_\\d+)(.html?)"); string target("https://www.ttufo.com/ufo/201705/154053_3.html"); string replace("http://www.ttufo.com/($1)/($2)/($3).htm($5)"); replace = "($1)($3)"; string s = boost::regex_replace( target, reg, replace, boost::match_default | boost::format_all); cout << "test replace 1" << endl; cout << s << endl; cout << "test replace1 end" << endl; } 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM