Boost logo

Boost Users :

From: Conway Allen (Allen.Conway_at_[hidden])
Date: 2007-03-16 03:48:43


Presumably it's because you're not anchoring the regexp to the start of the string - you're original regexp will work with 2 #s because it finds a match starting at the 2nd one. If you were to start your regexp thus: "^[#]P" then it would not say that the ## case was correct.
 

-----Message d'origine-----
De : boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] De la part de llwaeva_at_[hidden]
Envoyé : 16 March 2007 07:45
À : boost-users_at_[hidden]
Objet : [Boost-users] 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_results<std::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.

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net