Pages

boost::regex_match

As an introduction to some of the most interesting boost libraries you can read "Beyond the C++ Standard Library: An Introduction to Boost", by Björn Karlsson, an Addison Wesley Professional book. That's what I'm actually doing, and these are a few notes that I'm jotting down in the meanwhile.

The function boost::regex_match() accepts in input a string and a boost::regex, and returns a boolean to show if the string matches the regular expression.

Here is a few examples:

#include <iostream>
#include <string>
#include "boost/regex.hpp"

using std::cout;
using std::endl;
using std::string;
using boost::regex;
using boost::regex_match;

void r01()
{
// 1.
regex reg("(A.*)");

bool b = regex_match("A", reg);
if(b)
cout << "Just an A is enough to match the regex A.*" << endl;

string s("This expression does not match with A.*");
b = regex_match(s, reg);
if(b == false)
cout << s << endl;

s = "As this string starts with A, it matches";
b = regex_match(s, reg);
if(b == true)
cout << s << endl;

// 2.
reg = "\\d{3}";
s = "Three and only thee digits are expected to match the regex ";
b = regex_match(s, reg);
if(b == false)
cout << s << reg << endl;

s = "123";
b = regex_match(s, reg);
if(b == true)
cout << s << " matches" << endl;

// 2bis.
s += " ";
b = regex_match(s, reg);
if(b == false)
cout << '\"' << s << "\" does not match" << endl;

// 3.
reg = "[a-zA-Z]+";
s = "Only a single word is expected to match the regex ";
b = regex_match(s, reg);
if(b == false)
cout << s << reg << endl;

s = "ThisI5NotGood";
b = regex_match(s, reg);
if(b == false)
cout << s << endl;

s = "ThisIsOK";
b = regex_match(s, reg);
if(b == true)
cout << s << endl;

// 3bis.
reg = "\\w*";
s = "Only a single word is expected to match the regex ";
b = regex_match(s, reg);
if(b == false)
cout << s << reg << endl;

s = "AWordCou1dContainNumber5";
b = regex_match(s, reg);
if(b == true)
cout << s << endl;

// 4.
reg = ".*";
s = "Anything (&%5^!) would match the regex ";
b = regex_match(s, reg);
if(b == true)
cout << s << reg << endl;

// 5.
reg = "\\d{2}|N/A";
s = "Only two digits or the string 'N/A' would match the regex ";
b = regex_match(s, reg);
if(b == false)
cout << s << reg << endl;

s = "42";
b = regex_match(s, reg);
if(b == true)
cout << s << endl;

s = "N/A";
b = regex_match(s, reg);
if(b == true)
cout << s << endl;

// 6.
reg = "\\d{3}[a-zA-Z]+.(\\d{2}|N/A)\\s";
s = "123BlahBlahBlah%88 ";
b = regex_match(s, reg);
if(b == true)
cout << '\"' << s << "\" matches " << reg << endl;

s = "123%88 ";
b = regex_match(s, reg);
if(b == false)
cout << '\"' << s << "\" does not match" << endl;

s = "123X%88 ";
b = regex_match(s, reg);
if(b == true)
cout << '\"' << s << "\" matches" << endl;

s = "123X%N/A ";
b = regex_match(s, reg);
if(b == true)
cout << '\"' << s << "\" ditto" << endl;

// 7.
reg = "\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1";
s = "123Hello!N/A Hello";
if(b == true)
cout << '\"' << s << "\" matches " << reg << endl;
}

1. means: any string starting with a capital A
2. three digits
3. a single word containing only alphabetic characters (at least one)
3bis. a single word containing alphanumeric characters
4. anything (even an empty string)
5. two digits, or the string "N/A"
6. collates 2., 3., a single character, 5., and finally a blank
7. same as 6. but it requires at its end the same first pattern in rounded parenthesis

No comments:

Post a Comment