Boost logo

Boost Users :

Subject: Re: [Boost-users] [Regex] Search Substring
From: John Maddock (boost.regex_at_[hidden])
Date: 2011-03-08 11:40:00


> I am new to boost Libraries and I am trying to find a substring using
> Boost::Regex
>
> Lets say I want to find string "Hello" in "Hello world".
>
> std::string completeStr = "Hello World";
> std::string::const_iterator startOffset = completeStr.begin();
> std::string::const_iterator endOffset = completeStr.end();
>
> std::string searchWord= "Hello";
> boost::regex expression(searchWord);
>
> boost::match_flag_type flags = boost::match_any;
> while(boost::regex_search(startOffset, endOffset, what, expression,
> flags))
> {
> int32 position = what.position(1);
> int32 len = what[1].length();
> }
>
> but value of this position and length value is coming out to be -1.
> It is not able to find this substring, Could anyone tell me what I am
> doing
> wrong ?

First off, you don't need to use "match_any" unless it's what you really
mean.

You're then accessing *marked subexpression 1* inside the loop, but the
regular expression *does not contain any marked subexpressions*.

Use what.position() and what.length() to access the position and length info
of the match as a whole.

HTH, John.


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