Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/9c66c542b17fbbdfc8ba to your computer and use it in GitHub Desktop.
Save komasaru/9c66c542b17fbbdfc8ba to your computer and use it in GitHub Desktop.
C++ source code to match regular expressions(wchar) by boost.
/*
* 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