the Compartmented Robust Posix C++ Unit Test system

crpcut::regex

Match a string against a regular expression.

Used in: One of:

The predicate accepts either a C-string or a std::string as value to match against the regular expression.

crpcut::match<crpcut::regex>(pattern, flags = 0)

Parameters

pattern

The pattern string that is a regular expression. The pattern can be either a C-string or a std::string.

[Caution]Caution
Beware of C/C++ escape rules when regular expressions contain \.

flags

A combination using the bit-wise or (|) operator using zero or more of:

Flag

crpcut::regex::i

Ignore case when matching. Equal to REG_ICASE in regcomp().

[Note]Note
crpcut::regex::i does not take any locale into consideration.

crpcut::regex::e

Use extended regular expressions. Equal to REG_EXTENDED in regcomp()

crpcut::regex::m

Pattern is multi line. Equal to REG_NEWLINE in regcomp()

Example: the test program

     
     #include <crpcut.hpp>
     #include <string>
     
     std::string a_string()
     {
       return "192.168.0.1 is a valid IP address";
     }
     
     const char re[] = "^([0-9]{1,3}\\.){3}[0-9]{1,3}.*";
     
     TEST(begins_with_digits_and_dots)
     {
       const char num_dot_start[] = "^[0-9\\.]* ";
       ASSERT_TRUE(a_string() =~ crpcut::regex(num_dot_start));
     }
     
     TEST(begins_with_alpha)
     {
       const char alpha_start[] = "^[[:alpha:]]* ";
       ASSERT_TRUE(a_string() =~ crpcut::regex(alpha_start));
     }
     
     TEST(fail_non_extended)
     {
       ASSERT_PRED(crpcut::match<crpcut::regex>(re), a_string());
     }
     
     TEST(pass_extended)
     {
       ASSERT_PRED(crpcut::match<crpcut::regex>(re, crpcut::regex::e), a_string());
     }
     
     TEST(exception_what_string_mismatch)
     {
       ASSERT_THROW(std::string().at(3), std::exception, crpcut::regex(re));
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

      

fails three tests:


     FAILED!: begins_with_alpha
     phase="running"  --------------------------------------------------------------
     samples/regex.cpp:47
     ASSERT_TRUE(a_string() =~ crpcut::regex(alpha_start))
       is evaluated as:
         192.168.0.1 is a valid IP address =~ regex("^[[:alpha:]]* ")
     -------------------------------------------------------------------------------
     ===============================================================================
     FAILED!: fail_non_extended
     phase="running"  --------------------------------------------------------------
     samples/regex.cpp:52
     ASSERT_PRED(crpcut::match<crpcut::regex>(re), a_string())
       param1 = 192.168.0.1 is a valid IP address
     for crpcut::match<crpcut::regex>(re): regex("^([0-9]{1,3}\.){3}[0-9]{1,3}.*")
     
     -------------------------------------------------------------------------------
     ===============================================================================
     FAILED!: exception_what_string_mismatch
     phase="running"  --------------------------------------------------------------
     samples/regex.cpp:62
     ASSERT_THROW(std::string().at(3), std::exception, crpcut::regex(re))
     regex("^([0-9]{1,3}\.){3}[0-9]{1,3}.*") does not match what() == "basic_string::at: __n (which is 3) >= this->size() (which is 0)"
     -------------------------------------------------------------------------------
     ===============================================================================
     5 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       2          2              0
     FAILED   :       3          3              0