Boost logo

Boost Users :

From: Eric Niebler (eric_at_[hidden])
Date: 2006-04-12 18:56:24


Lynn Allan wrote:
> Is the following getting closer to "best practice" to use
> xpressive-static for the kinds of specialized searches I'm attempting.
> When I'm further along on the learning curve, I'll try to prepare some
> "apples and apples" timing comparisons between boost::regex,
> xpressive, spirit, a hand-tuned state-machine searcher, and an
> automatic FSM generator utility.
>
> I think I'm conforming to the guidelines on this page:
> X:\DevTools\Boost\libs\xpressive\doc\html\boost_xpressive\user_s_guide\tips_n_tricks.html
> except I don't understand how to specify
> syntax_option_type::optimize
> (does it apply to xpressive-static, or only xpressive-dynamic)?

Static regexes assume the optimize flag.

> boost::xpressive::regex_constants::syntax_option_type flags =
> boost::xpressive::regex_constants::optimize;
> sregex rexStatic =
> (s1 = (as_xpr("Sunday") | "Sun")) |
> (s2 = (as_xpr("Monday") | "Mon")) |
> (s3 = (as_xpr("Tuesday") | "Tue")) |
> (s4 = (as_xpr("Wednesday") | "Wed")) |
> (s5 = (as_xpr("Thursday") | "Thu")) |
> (s6 = (as_xpr("Friday") | "Fri")) |
> (s7 = (as_xpr("Saturday") | "Sat")) ;

Yep, you got it.

> //??? rexStatic.compile(rexStatic, flags);
> smatch what;
>
> std::string testStr =
> "Alternate days of the week are Tue and Thursday and Sat and
> Monday. "
> "And then Monday and Wed and Friday and Sun. ";
> std::string::const_iterator start = testStr.begin();
> std::string::const_iterator finish = testStr.end();
> int foundCount = 0;
> while (regex_search(start, finish, what, rexStatic)) {
> foundCount++;
> size_t limit = what.size();
> size_t matchIndex = 1;
> while (matchIndex <= limit) {
> if (what[matchIndex].matched == true) {
> break;
> }
> ++matchIndex;
> }
> #ifdef _DEBUG
> cout << "FoundCount: " << foundCount
> << " Index: " << static_cast<int>(matchIndex)
> << " what[0]: " << what[0] << endl;
> #endif
> start = what[0].second;
> }

Ah, you're trying to find all the matches. Check out regex_iterator.

sregex_iterator begin(testStr.begin(), testStr.end(), rexStatic), end;
for(; begin != end; ++begin, ++foundCount) {
     smatch const &what = *begin;
     size_t limit = what.size();
     size_t matchIndex = 1;
     for(; matchIndex <= limit; ++matchIndex)
         if(what[matchIndex].matched == true)
             break;
#ifdef _DEBUG
     cout << "FoundCount: " << foundCount
          << " Index: " << static_cast<int>(matchIndex)
          << " what[0]: " << what[0] << endl;
#endif
}

HTH,

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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