|
Boost Users : |
From: llwaeva_at_[hidden]
Date: 2007-03-16 07:00:09
>> 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: "(?<!#)#" will match any single "#" that is *not* preceded by another #.
> The previous character (whatever it is) >does not form part of the result.
>John.
Thank you so much. It's amazing! I see that regex is really a powerful tool!
BTW, in order to replace the pattern #P and avoid the string start with double #, I find it is all
right to specify the exact times in matching when using replace_all_regex, for example
replace_all_regex( source, boost::regex("([#]\{1\}P)|([#]\{1\}G)"), string("(?1 FIRST)(?2 SECOND)"), format_all );
of course, using the "negative lookbehind" approach also works in this problem
replace_all_regex( source, boost::regex("((?<!#)#P)|((?<!#)#G)"), string("(?1 FIRST)(?2 SECOND)"), format_all );
For my case, the program might be used to handle a LARGE text (perhaps several megabytes). So, I am wondering which
approach is faster!
Thanks again!
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