1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include "driver.h"
#include <bobcat/pattern>
using namespace std;
using namespace FBB;
#include <algorithm>
#include <cstring>
void showSubstr(string const &str)
{
static int count = 0;
cout << "String " << ++count << " is '" << str << "'\n";
}
void match(Pattern const &patt, string const &text)
try
{
Pattern pattern{ patt };
pattern.match(text);
Pattern p3(pattern);
cout << "before: " << p3.before() << "\n"
"matched: " << p3.matched() << "\n"
"beyond: " << pattern.beyond() << "\n"
"end() = " << pattern.end() << '\n';
for (size_t idx = 0; idx != pattern.end(); ++idx)
{
string str = pattern[idx];
if (str.empty())
cout << "part " << idx << " not present\n";
else
{
Pattern::Position pos = pattern.position(idx);
cout << "part " << idx << ": '" << str << "' (" <<
pos.first << "-" << pos.second << ")\n";
}
}
}
catch (exception const &exc)
{
cout << exc.what() << '\n';
}
int main(int argc, char **argv)
{
string patStr = R"(\d+)";
do
{
cout << "Pattern: '" << patStr << "'\n";
try
{
// by default: case sensitive
// use any args. for case insensitive
Pattern patt(patStr, argc == 1);
cout << "Compiled pattern: " << patt.pattern() << '\n';
while (true)
{
cout << "string to match : ";
string text;
getline(cin, text);
if (text.empty())
break;
cout << "String: '" << text << "'\n";
match(patt, text);
}
}
catch (exception const &exc)
{
cout << exc.what() << ": compilation failed\n";
}
cout << "New pattern: ";
}
while (getline(cin, patStr) and not patStr.empty());
}
|