why the following regexp doesn't meet my requirement?
Hi there, First of all, sorry for so many questions on using of regex. Actually, I am a beginner of regular expression; it is hard for me to understand some terms in document. I am going to extract a string in some pattern like #P{EXP_BEING_EXTRACTED} here, EXP_BEING_EXTRACTED is any expression which I want. For instance, xyz in #P{xyz} and 123_456 in #P{123_456}. But, I also need to ignore the case with double # as prefix, i.e. the output of ##P{123} should be kept unchanged. I use regex_search string source; // source contains the text being handled #P{x} or ##P{x} may occur boost::regex regexp; string::const_iterator start, end; boost::match_resultsstd::string::const_iterator what; boost::match_flag_type flags = boost::match_default; regexp = "[#]\{1\}P[{]\([^}]*\)[}]"; start = source.begin(); end = source.end(); if ( regex_search(start, end, what, regexp, flags) ) { // get the expression inside {} here // 1) get the text: string( what[1].first, what[1].second) // 2) replace the whole matched string, i.e., replace string(what[0].first, what[1].second); } The code above is fine for #P{x}; however, for the case ##P{x} regex_search still returns true. It seems that the regex_search firstly found the ##P{x} which is not match the regular expression; then, it go ahead from the second # and do the match again, so it found #P{x} finally. I don't know if the process is similiar to what I depict. Anyway, I just want to know how can I ignore the case ##P{x}. I try to modify the regular expression as follow regexp = "[^#]#P[{]\([^}]*\)[}]"; But, as I mentioned above, the whole matached string, namely #P{x}, will be replaced once the match is found. So, the character [^#] will also be replaced, that is not what I want.
llwaeva@21cn.com wrote:
But, as I mentioned above, the whole matached string, namely #P{x}, will be replaced once the match is found. So, the character [^#] will also be replaced, that is not what I want.
Try a negative lookbehind: "(?
participants (2)
-
John Maddock
-
llwaeva@21cn.com