views:

315

answers:

1

Folks, desperately need your help. I am using C++ VS 2008 to strip RTF tags to make search in the RTF text only.

The rtf text I get from CRichTextEdit into CString and it works just fine. Here how I try to remove the tags:

1 std::tr1::cmatch res;
2 std::string str = note;
3 const std::tr1::regex rx("({\\)(.+?)(})|(\\)(.+?)(\b)");
4 std::string replacement = "";
5 std::string result = std::tr1::regex_replace(str,rx,replacement);
6 
7 CString strSearchText = result.c_str();

The line 3 crashes on the pattern above. A simple pattern for a text replacement works just fine: Ex replace "fast" from "fast fox" with an emplty string. the crash message is here:

First-chance exception at 0x7622fbae in ICView.exe: Microsoft C++ exception: std::tr1::regex_error at memory location 0x0012ee20..
First-chance exception at 0x7622fbae in ICView.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
Unhandled exception at 0x7622fbae in ICView.exe: Microsoft C++ exception: std::tr1::regex_error at memory location 0x0012ee20..

thanks a lot. Val

A: 

You need to escape your backslashes, since they serve as an escape character both in regex and in C++ string literals. This means that to match a single backslash, you need four backslash characters. I think this should work: "({\\\\)(.+?)(})|(\\\\)(.+?)(\\b)"

This should fix the exception, but if it still happens you can catch it and use the exception object's what() or code() methods to see what the problem is.

Edit: the braces should also be escaped since they are special characters in regex. so use:

"(\\{\\\\)(.+?)(\\})|(\\\\)(.+?)(\\b)"
interjay
You are absolutely right pointing out to the backslashes as escape chars - thanks I will change them.But the problem is not in not properly replacing my strings with new patterns but rather crashing on the line 3 where I declare rx pattern. The std::tr1::regex_error is thrown as poiniting to wrong memory loccation or so. If I declare const std::tr1::regex rx("brown fox"); it will work like a charm. What would be wrong?
val
The backslashes are what's wrong. They cause the parantheses to be escaped, which causes an unmatched parantheses error.
interjay
Guys, thanks a million. Of cause it was the braces that must have been escaped in regex!!!! I stupidly forgot about those #$#$#$#@@Cheers and happy coding,Val :-)
val