[Regex] Search Substring

Hi, 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 ? Subhash

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.

Thanks a lot John for this, I changed my code to this and it worked..
boost::smatch what;
while (boost::regex_search(startOffset ,endOffset , what, expression))
{
int32 pos = what.position();
int32 len = what.length();
}
However, could you tell me how to do this if my input string is "Hello
World, Hello".
that is the reason I am using a while loop instead of an if since my input
string can contain more than 1 search string.
Subhash
On Tue, Mar 8, 2011 at 4:40 PM, John Maddock
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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 08/03/2011 18:09, S Nagre wrote:
Thanks a lot John for this, I changed my code to this and it worked..
boost::smatch what; while (boost::regex_search(startOffset ,endOffset , what, expression)) { int32 pos = what.position(); int32 len = what.length(); }
However, could you tell me how to do this if my input string is "Hello World, Hello". that is the reason I am using a while loop instead of an if since my input string can contain more than 1 search string.
I'd say startOffset += pos + len;
participants (3)
-
John Maddock
-
Mathias Gaunard
-
S Nagre