views:

96

answers:

2

Good programmers keep simple things easy right? And it's not like the boost documentation makes your life less uneasy...

All I want is an implementation for:

// fulfils the function of a regex matching where the pattern may match a
// substring instead of the entire string
bool search( std::string, std::string, SomeResultType )

So it can be used as in:

std::string     text, pattern;
SomeResultsType match;

if( search( text, pattern, match ) )  
{
   std::string result      = match[0];

   if( match[1].matched )
      // where this is the second capture group, not recapturing the same group
      std::string secondMatch = match[1];

}

I want my client code not to be bothered with templates and iterators... I know, I'm a wuss. After peering for an hour over the template spaghetti in the boost docs for doing something so simple, I feel like my productivity is seriously getting hampered and I don't feel like I've learned anything from it.

boost::regex_match does it pretty simple with boost::cmatch, except that it only matches the whole string, so I've been adapting all my patterns to match the whole strings, but I feel that it is a dirty hack and would prefer some more proper solution. If I would have known it would take this long, I would have stuck with regex_match

Also welcome, a copy of Reading boost documentation for dummies

Next week in Keep it simple and easy with boost, function binders! No, just kidding, I wouldn't do that to anyone.

Thanks for all help

A: 

I think you want regex_search: http://www.boost.org/doc/libs/1_44_0/libs/regex/doc/html/boost_regex/ref/regex_search.html

Probably this overload is the one you want: bool regex_search(const basic_string& s, match_results::const_iterator, Allocator>& m, const basic_regex& e, match_flag_type flags = match_default);

That seems to match what you wanted - SomeResultsType is smatch, and you need to convert your pattern to a regex first.

Alan Stokes
ufotds
Why don't you simply wrap the boost function call with a function of your own that does the search and returns the results by reference to a vector?
Dakota Hawkins
@Dakota: that is actually not such a bad idea. Especially since I would only have to write it once ever... A vector would lose some of the functionality of match_results, but I don't think I'm going to need that alot anyways.
ufotds
std::string(what[6].first, what[6].second) seems overly complicated.what[6] has an implicit conversion to std::string, or you can if you need to explicitly call what[6].str().
Alan Stokes
A: 

On Windows, you can use the .NET Regex class:

Example (copied from the linked page):

#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
int main()
{
   // Define a regular expression for repeated words.
   Regex^ rx = gcnew Regex( "\\b(?<word>\\w+)\\s+(\\k<word>)\\b",static_cast<RegexOptions>(RegexOptions::Compiled | RegexOptions::IgnoreCase) );

   // Define a test string.        
   String^ text = "The the quick brown fox  fox jumped over the lazy dog dog.";

   // Find matches.
   MatchCollection^ matches = rx->Matches( text );

   // Report the number of matches found.
   Console::WriteLine( "{0} matches found.", matches->Count );

   // Report on each match.
   for each (Match^ match in matches)
   {
      String^ word = match->Groups["word"]->Value;
      int index = match->Index;
      Console::WriteLine("{0} repeated at position {1}", word, index);   
   }
}
Peter G.
Hi, thanks for the effort, but I prefer not to use .NET, because it is not portable and also because even on windows as a user I hate their side by side policy. It's not like your app depends on just another dll being shipped with it. Boost doesn't even require an extra dll. I prefer not to install another third party lib for something boost already (does/should do), but I would use any third party lib before I would turn to .NET
ufotds