Hi John "(?!foo)bar" matches "bar" in the last three lines of the following: foobar ??bar barfoo bar?? bar Why doesn't it match "bar" in all the lines? Yutaka Emura ---------------
I am using RegEx in Boost v1.29.0. (?= ) and (?! ) expressions do not seem to work at a certain condition.
In the following example, "(?=a)b" should match "b" in "xyzabcdefg", but does not.
Which is correct: (?=a) is a forward lookahead *possitive* assertion, so (?=a)b can never match anything since the next character can't be both an 'a' and a 'b' at the same time!
Similarly, "(?!a)b" should not match "b" in "xyzabcdefg" but it does.
It should match: (?!a) will match anything except 'a' so (?!a)b will match the next 'b' that it finds.
"(?!foo)bar" matches "bar" in the last three lines of the following:
foobar ??bar barfoo bar?? bar
Why doesn't it match "bar" in all the lines?
Provided you are calling regex_search it should find a match in all the cases, if it doesn't then please submit a test case. John Maddock http://ourworld.compuserve.com/homepages/john_maddock/index.htm
Dr John Maddock, Here is a test case. int main(int argc, char* argv[]) { boost::regex expression; std::string asFind[5]; expression.assign("(?!foo)bar", boost::regbase::perl); asFind[0] = "foobar"; asFind[1] = "??bar"; asFind[2] = "barfoo"; asFind[3] = "bar??"; asFind[4] = "bar"; for( int i = 0; i < 5; i++ ){ std::string s = asFind[i]; std::string::const_iterator start = s.begin(); std::string::const_iterator end = s.end(); boost::match_resultsstd::string::const_iterator subexp; if (boost::regex_search( start, end, subexp, expression, boost::match_default)) { printf( "%s - Found.\n", s.c_str() ); } else { printf( "%s - Not Found.\n", s.c_str() ); } } return 0; } The output is: foobar - Not Found. ??bar - Not Found. barfoo - Found. bar?? - Found. bar - Found. However, all the words should be "Found". Thank you for your time in advance. Yutaka Emura -----Original Message-----
"(?!foo)bar" matches "bar" in the last three lines of the following:
foobar ??bar barfoo bar?? bar
Why doesn't it match "bar" in all the lines?
Provided you are calling regex_search it should find a match in all the cases, if it doesn't then please submit a test case.
participants (2)
-
John Maddock
-
Yutaka Emura