diff --git a/base/base_tests/regexp_test.cpp b/base/base_tests/regexp_test.cpp index 7e0e0dea35..7621e1a5da 100644 --- a/base/base_tests/regexp_test.cpp +++ b/base/base_tests/regexp_test.cpp @@ -2,6 +2,8 @@ #include "../regexp.hpp" +#include "../../base/stl_add.hpp" + UNIT_TEST(RegExp_Or) { @@ -20,3 +22,57 @@ UNIT_TEST(RegExp_Or) TEST(!regexp::IsExist("Aruba.mwm.resume3", exp), ()); TEST(!regexp::IsExist("Aruba.mwm.resume.tmp", exp), ()); } + +UNIT_TEST(RegExp_ForEachMatched) +{ + regexp::RegExpT exp; + regexp::Create("-?\\d+\\.?\\d*, *-?\\d+\\.?\\d*", exp); + + { + string const s = "6.66, 9.99"; + vector v; + regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v)); + TEST_EQUAL(v.size(), 1, ()); + TEST_EQUAL(v[0], s, ()); + } + + { + string const s1 = "6.66, 9.99"; + string const s2 = "-5.55, -7.77"; + vector v; + regexp::ForEachMatched(s1 + " 180 , bfuewib 365@" + s2, exp, MakeBackInsertFunctor(v)); + TEST_EQUAL(v.size(), 2, ()); + TEST_EQUAL(v[0], s1, ()); + TEST_EQUAL(v[1], s2, ()); + } + + { + string const s = "X6.66, 9.99"; + vector v; + regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v)); + TEST_EQUAL(v.size(), 1, ()); + TEST_EQUAL(v[0], string(s.begin() + 1, s.end()), ()); + } + + { + string const s = "6.66, 9.99X"; + vector v; + regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v)); + TEST_EQUAL(v.size(), 1, ()); + TEST_EQUAL(v[0], string(s.begin(), s.end() - 1), ()); + } + + { + string const s = "6.66X, 9.99"; + vector v; + regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v)); + TEST_EQUAL(v.size(), 0, ()); + } + + { + string const s = "6.66, X9.99"; + vector v; + regexp::ForEachMatched(s, exp, MakeBackInsertFunctor(v)); + TEST_EQUAL(v.size(), 0, ()); + } +} diff --git a/base/regexp.hpp b/base/regexp.hpp index 171c365684..0425921d7d 100644 --- a/base/regexp.hpp +++ b/base/regexp.hpp @@ -1,6 +1,8 @@ #pragma once #include +#include + namespace regexp { @@ -15,4 +17,14 @@ namespace regexp { return boost::xpressive::regex_search(str, regexp); } + + template void ForEachMatched(string const & str, RegExpT const & regexp, FnT fn) + { + typedef boost::xpressive::sregex_token_iterator IterT; + + IterT i(str.begin(), str.end(), regexp); + IterT end; + for (; i != end; ++i) + fn(*i); + } }