boost::regex snippet [solved]

Started by
5 comments, last by Endar 18 years, 1 month ago
I'm attempting to get this little boost::regex snippet to work.

boost::cmatch match;
boost::regex expression("true|false");
if( boost::regex_match( "true", match, expression ) ){
	printf("\n Found A match!!\n");
	std::string s;
	s.assign( match[3].first, match[3].second );

	printf("text: %s\n", s.c_str() );
}
And please don't tell me to post on the boost mailing lists. It's not working for me and I'm attempting to figure out why, but as a fallback for the mailing lists, I have no other place to ask my boost questions. This code snippet is the only code inside my main function. It always prints that it's found a match, but never prints the text. If I print first string char I get the null character. I'm not sure but from some things I've heard, this could actually be added by the c_str() function and not be included in the actual std::string data, so I don't know that tells me much. I don't think it could be the regular expression, otherwise regex_match should be returning false, right? I tried looking in the boost headers for some tips on the cmatch type but that didn't provide me with much info that I could use. Anyone know what I'm doing wrong? [Edited by - Endar on March 19, 2006 10:38:14 AM]
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
Humm... You are trying to read the third captured subexpression (match[3]), but the problem is that there's no such thing! In fact, there are no capturing groups (parentheses-separated regions) at all in the expression. The resulting sub_match object is therefore empty, and nothing is assigned to the string. If you want the whole matched region, you want match[0], not match[3].
First off, I changed the match index to '0'.

Well I've done some more experimenting and I've found that I'll only get a proper result if the only thing in the input string is the string that matches the expression.

Is that right? Because I'll run the program with "[0-9]+" as an expression and "12 3" as the input string and boost::regex_match won't return 'true'. If I get rid of the " 3", then there's no problem, it all works perfectly.

I haven't looked at parsers for a while, but is this what they're supposed to do? Return a match iff the input string matches the expression? I was under the impression that they would take a large string and return the part that matches the expression. Or is that a lexer?

Edit:: Do I need the parenthesis '(' and ')' in the regular expression?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Quote:Original post by Endar
I haven't looked at parsers for a while, but is this what they're supposed to do? Return a match iff the input string matches the expression? I was under the impression that they would take a large string and return the part that matches the expression. Or is that a lexer?

Try boost::regex_search instead :)
Quote:Original post by Endar
First off, I changed the match index to '0'.

Well I've done some more experimenting and I've found that I'll only get a proper result if the only thing in the input string is the string that matches the expression.

Is that right? Because I'll run the program with "[0-9]+" as an expression and "12 3" as the input string and boost::regex_match won't return 'true'. If I get rid of the " 3", then there's no problem, it all works perfectly.

I haven't looked at parsers for a while, but is this what they're supposed to do? Return a match iff the input string matches the expression? I was under the impression that they would take a large string and return the part that matches the expression. Or is that a lexer?

Edit:: Do I need the parenthesis '(' and ')' in the regular expression?

"[0-9]+" indicates that you are attempted to match 1 or more numbers. In your example string, you have two numbers, followed by a space. Once it hits that space, the match fails, and you get a return value of false.

What you might want to try is regex_search instead. As I recall, it looks for the first match, rather than attempting to make the entire string match. In your case, it would yield "12". I don't remember how it indicates that, though, sorry.

CM
Thanks guys. It turns out that it doesn't matter. I'm using it for an in engine console, and the only thing that will turn out to have spaces in it will be a string and I'll have to do something special for that.

At the moment, to match up a float value, an integer value and a boolean value, I'm just using std::istringstream to stream out a word (sequence of characters seperated by white space) at a time.

I will have to mess around with that to read out properly for strings. I might make it so that strings have to be bounded by quotation marks, so I can just stream the rest out of the string stream and concatenate it all together.

Anyway, we'll see what happens. :D
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Aha, I think I'm all done with boost::regex problems.

I'm simply grabbing the whole user input string and parsing it with boost::regex_search with an expression of ("\".*\"") . This means that regex_search will return the position of the part of the string when there is a string bounded by quotation marks. Eg. "Hi, I'm a string" is counted as a string. Anything not bounded by quotation marks is not considered a string.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper

This topic is closed to new replies.

Advertisement