开发者

boost::regex_search - boost kills my brain cells, again

开发者 https://www.devze.com 2023-01-20 14:33 出处:网络
Good programmers keep simple things easy right? And it\'s not like the boost documentation makes your life less uneasy...

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]开发者_如何学JAVA;

}

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


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.


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);   
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消