Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Environment: Windows 7 (32 bit), Visual Studio 2010 (Visual C++)

I am trying to use Boost REGEX regex_match algorithm.
I am using its grep flag.
My code is as follows and here (a)* matches string "aaa", but still b comes out to be false.




bool MatchWithGrep(string& pattern, string& data)
{

	const boost::regex e(pattern.c_str(), boost::regex::grep);
	return regex_match(data.c_str(), e);

}

I am passing following parameters
bool b = MatchWithGrep("(a)*\\n123", "aaa");




Can any one help me to find whats wrong with my code?
Thanks in advance.
Posted
Updated 26-Dec-13 5:58am
v2

1 solution

No - but your data and the pattern do not match.
You are searching for "zero or more 'a' followed by a newline character and the string '123'" which is not present in your sample data of "aaa"
Try calling it this way:
C++
bool b = MatchWithGrep("(a)*", "aaa\\n123");
And it might work better...
 
Share this answer
 
Comments
Member 10329703 26-Dec-13 5:29am    
OriginalGriff,

In Boost Help, following is documented.

Link -
http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/syntax/basic_syntax.html

When an expression is compiled with the flag grep set, then the expression is treated as a newline separated list of POSIX-Basic expressions, a match is found if any of the expressions in the list match, for example:
boost::regex e("abc\ndef", boost::regex::grep);
will match either of the POSIX-Basic expressions "abc" or "def".

Considering this, in my example i have 2 basic expressions
(a)* and 123. Presence of any should give me bool b as true.
And my data is "aaa". So it should give true. But i am getting false.
OriginalGriff 26-Dec-13 5:59am    
Ah! But that isn't what you have... Your code:
bool b = MatchWithGrep("(a)*\\n123", "aaa");
passes a pattern without a newline:
(a)*\n123
because the '\\' is escaped to a single backslash character.
Try
bool b = MatchWithGrep("(a)*\n123", "aaa");
Instead...
Member 10329703 26-Dec-13 7:01am    
Since its c++ string \\n will be taken as \n internally.
And if i give "(a)*\n123" it will be read as "(a)*n123".
I had debugged the code.
OriginalGriff 26-Dec-13 7:05am    
No, it won't.
"\\n" is two characters: a '\' character followed by a 'n' character. You need (from the sample in the article you linked to) a single '\n' character as it wants newline separated strings.
And "(a)*\n123" is two such strings: "(a)*" and "123" separated by a newline character.
Member 10329703 26-Dec-13 9:43am    
I had tried this solution and had debugged the code, it shows value for pattern variable as "(a)*n123" in watch window. Also result comes out to be false for variable b.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900