|
Boost Users : |
Subject: [Boost-users] boost::regex - I do not understand why this test fails.
From: Mauricio Gomes (mgpensar_at_[hidden])
Date: 2010-06-22 21:53:50
Hi,
Below it is a self contained example and the output it produces.
I thought I would get a match for REGEX2 but not for REGEX1 in string s.
However REGEX1 matches and produces weird (for me) results.
I would expect REGEX1 only matches for strings with 6 terms (3 words
and 3 numbers).
I know this probably is just a regex (lack of ) knowledge problem on
my part and not a boost::regex problem but I could not find a better
forum than this one to ask for help.
Thank you in advance for your attention !
Mau.
#define BOOST_TEST_MODULE test_module_example
#include <boost/test/included/unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <string>
BOOST_AUTO_TEST_SUITE(example_suite)
BOOST_AUTO_TEST_CASE(example_test)
{
boost::cmatch res;
std::string s = " JETIX 2,957,081 643,225.51 0.00"; // tabs
separating the terms.
static const std::string& REGEX1 =
"\\s*(\\w+)\\s+([\\d,\\.]+)\\s+([\\d,\\.]+)\\s([\\d,\\.]+)\\s*$";
static const std::string& REGEX2 =
"\\s*(\\w+){1}\\s*(\\w+){1}\\s*(\\w+){1}\\s+([\\d,\\.]+){1}\\s+([\\d,\\.]+){1}\\s([\\d,\\.]+){1}\\s*$";
boost::regex rx(REGEX2);
if (boost::regex_match (s.c_str(), res, rx))
{
BOOST_CHECK_EQUAL ("JETIX" , res[1]);
BOOST_CHECK_EQUAL ("2,957,081" , res[2]);
BOOST_CHECK_EQUAL ("643,225.51", res[3]);
BOOST_CHECK_EQUAL ("0.00" , res[4]);
}
else
{
BOOST_FAIL ("It was supposed to match.");
}
boost::cmatch res2;
boost::regex rx2(REGEX1);
bool match = boost::regex_match (s.c_str(), res2, rx2);
for (unsigned i = 0; i < res2.size (); ++i)
std::cout << res2[i] << std::endl;
if (match) // IT FAILS HERE.
BOOST_FAIL ("It was NOT supposed to match.");
}
BOOST_AUTO_TEST_SUITE_END()
--------------------------------------------------------------------------------------
Output:
Running 1 test case...
JETIX 2,957,081
643,225.51 0.00
JET
I
X
2,957,081
643,225.51
0.00
C:/projects/test/main2.cpp(40): fatal error in "example_test": It was
NOT supposed to match.
*** 1 failure detected in test suite "test_module_example"
Process returned 201 (0xC9) execution time : 0.250 s
Press any key to continue.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net