On Thursday 07 November 2002 10:44 am, Lars Gullik Bjønnes wrote:
> | Actually, I was hoping you'd just volunteer ;-)
> already did it...

Good man!

> #include <boost/regex.hpp>
> #include <string>
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
>       string litex(".|*?+(){}[]^$\\");
>       string fmt("\\\\$&");
>       boost::RegEx reg("[\\.\\|\\*\\?\\+\\(\\)\\{\\}\\[\\]\\^\\$\\\\]");
>       string res = reg.Merge(litex, fmt);
>       cout << "In: " << litex << "\nOut: " << res << endl;
> }
>
> ./merge
> In: .|*?+(){}[]^$\
> Out: \.\|\*\?\+\(\)\{\}\[\]\^\$\\
>
> This should be pretty close right?

Perfect. Shame boost isn't as clever as sed. This is what Peter Tiller on the 
sed-users list had to say:

========================================================
Also this should work on any correct version of sed too:

$ echo '. | * ? + ( ) { } [ ] ^ $ \' | sed
's;\([]\.[|*?+(){}^$]\);\\\1;g'
\. \| \* \? \+ \( \) \{ \} \[ \] \^ \$ \\

$

Why?  Because meta-characters aren't treated as such inside a bracket
expression.  The only metacharacters inside a bracket expression in BREs
should be caret ^, but then only if it appears _first_, after the [, and
hyphen -, to show a range such as a-z, but then only if it _can_ show a
range, i.e., [a-z] is a range but [a-] isn't.

Why didn't I mention it before?  I forgot!
========================================================

boost, it would appear ain't that clever. We have to escape the 
meta-characters within the bracket expression too. Ah well ;-)

Angus

Reply via email to