| the Compartmented Robust Posix C++ Unit Test system | hosted by |
|---|
Match a string against a regular expression.
Used in: Template parameter to
crpcut::match<matcher>(...) in an
ASSERT_PRED(pred, ...) check.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
patternThe pattern string that is a regular expression. The pattern can be either a C-string or a std::string.
![]() | Note |
|---|---|
| Beware of C/C++ escape rules when regular expressions contain “\”. |
flagsA combination using the bit-wise or (|) operator using zero or more of:
Flag
iIgnore case when matching. Equal to
REG_ICASE in
regcomp().
![]() | Note |
|---|---|
crpcut::regex::i
does not take any locale into consideration. |
eUse extended regular expressions. Equal to
REG_EXTENDED in
regcomp()
mPattern is multi line. Equal to
REG_NEWLINE in
regcomp()
Example: the test program
#include <crpcut.hpp>
#include <string>
std::string name()
{
return "192.168.0.1 is a valid IP address";
}
TEST(fail_non_extended)
{
ASSERT_PRED(crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*"),
name());
}
TEST(pass_extended)
{
ASSERT_PRED(crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*",
crpcut::regex::e),
name());
}
int main(int argc, char *argv[])
{
return crpcut::run(argc, argv);
}
fails one test:
FAILED: fail_non_extended
phase="running" --------------------------------------------------------------
/home/bjorn/devel/crpcut/doc-src/samples/regex.cpp:39
ASSERT_PRED(crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*"), name())
param1 = 192.168.0.1 is a valid IP address
crpcut::match<crpcut::regex>("^([0-9]{1,3}\\.){3}[0-9]{1,3}.*") :
did not match
-------------------------------------------------------------------------------
===============================================================================
Total 2 test cases selected
UNTESTED : 0
PASSED : 1
FAILED : 1