C++ source code to match regular expressions(wchar) by boost.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Matching regular expressions by boost. (for wchar) | |
*/ | |
#include <iostream> | |
#include <string> | |
#include <boost/regex.hpp> // require "boost-regex-dev" | |
using namespace std; | |
/* | |
* [CLASS] Process | |
*/ | |
class Proc | |
{ | |
// Private Declaration | |
string sSrc, sPtnA, sPtnS; // Source string, Regex pattern (string) | |
boost::regex reA, reS; // Regular expression | |
boost::smatch sm; // Match result (string) | |
string::const_iterator start, end; // Iterator | |
bool regexStringAll(); // All matches (string) | |
bool regexRangeAll(); // All matches (range) | |
bool regexStringSub(); // Sub matches (string) | |
bool regexRangeSub(); // Sub matches (range) | |
public: | |
Proc(); // Constructor | |
bool execMain(); // Main Process | |
}; | |
/* | |
* Proc - Constructor | |
*/ | |
Proc::Proc() | |
{ | |
// Initial settings | |
sSrc = "これは正規表現のテストです。"; | |
sPtnA = "(.*)(正規表現)(.*)"; | |
sPtnS = "正(.*)現"; | |
reA = sPtnA; | |
reS = sPtnS; | |
cout << "[Source string ] " << sSrc << "\n" | |
<< "[Regex pattern(All)] " << sPtnA << "\n" | |
<< "[Regex pattern(Sub)] " << sPtnS << "\n" | |
<< endl; | |
} | |
/* | |
* Main Process | |
*/ | |
bool Proc::execMain() | |
{ | |
try { | |
// All mathces (string) | |
if (!regexStringAll()) return false; | |
// All mathces (range) | |
if (!regexRangeAll()) return false; | |
// Sub mathces (string) | |
if (!regexStringSub()) return false; | |
// Sub mathces (range) | |
if (!regexRangeSub()) return false; | |
} catch (char *e) { | |
cerr << "[EXCEPTION] " << e << endl; | |
return false; | |
} | |
return true; | |
} | |
// All matches (string) | |
bool Proc::regexStringAll() | |
{ | |
cout << "* All match - string\n"; | |
try { | |
if (boost::regex_match(sSrc, sm, reA)) { | |
cout << " ==== Matched ====\n"; | |
for (size_t i = 0; i < sm.size(); ++i) { | |
cout << " [" << i << "] (pos = " << sm.position(i) << ", " | |
<< "len = " << sm.length(i) << ") " | |
<< sm.str(i) << "\n"; | |
} | |
} else { | |
cout << " ==== Unmatched ====\n"; | |
} | |
} catch (char *e) { | |
cerr << "[EXCEPTION] " << e << endl; | |
return false; | |
} | |
cout << endl; | |
return true; | |
} | |
// All matches (range) | |
bool Proc::regexRangeAll() | |
{ | |
cout << "* All match - range\n"; | |
try { | |
start = sSrc.begin(); | |
end = sSrc.end(); | |
if (boost::regex_match(start, end, sm, reA)) { | |
cout << " ==== Matched ====\n"; | |
for (size_t i = 0; i < sm.size(); ++i) { | |
cout << " [" << i << "] (pos = " << sm.position(i) << ", " | |
<< "len = " << sm.length(i) << ") " | |
<< sm.str(i) << "\n"; | |
} | |
} else { | |
cout << " ==== Unmatched ====\n"; | |
} | |
} catch (char *e) { | |
cerr << "[EXCEPTION] " << e << endl; | |
return false; | |
} | |
cout << endl; | |
return true; | |
} | |
// Sub matches (string) | |
bool Proc::regexStringSub() | |
{ | |
cout << "* Sub match - string\n"; | |
try { | |
if (boost::regex_search(sSrc, sm, reS)) { | |
cout << " ==== Matched ====\n"; | |
for (size_t i = 0; i < sm.size(); ++i) { | |
cout << " [" << i << "] (pos = " << sm.position(i) << ", " | |
<< "len = " << sm.length(i) << ") " | |
<< sm.str(i) << "\n"; | |
} | |
} else { | |
cout << " ==== Unmatched ====\n"; | |
} | |
} catch (char *e) { | |
cerr << "[EXCEPTION] " << e << endl; | |
return false; | |
} | |
cout << endl; | |
return true; | |
} | |
// Sub matches (range) | |
bool Proc::regexRangeSub() | |
{ | |
cout << "* Sub match - range\n"; | |
try { | |
start = sSrc.begin(); | |
end = sSrc.end(); | |
if (boost::regex_search(start, end, sm, reS)) { | |
cout << " ==== Matched ====\n"; | |
for (size_t i = 0; i < sm.size(); ++i) { | |
cout << " [" << i << "] (pos = " << sm.position(i) << ", " | |
<< "len = " << sm.length(i) << ") " | |
<< sm.str(i) << "\n"; | |
} | |
} else { | |
cout << " ==== Unmatched ====\n"; | |
} | |
} catch (char *e) { | |
cerr << "[EXCEPTION] " << e << endl; | |
return false; | |
} | |
cout << endl; | |
return true; | |
} | |
/* | |
* Execution | |
*/ | |
int main(){ | |
try { | |
Proc objMain; | |
bool bRet = objMain.execMain(); | |
if (!bRet) cout << "ERROR!" << endl; | |
} catch (char *e) { | |
cerr << "[EXCEPTION] " << e << endl; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment