tags:

views:

90

answers:

1

The question is: http://stackoverflow.com/questions/3978351/how-to-avoid-backslash-escape-when-writing-regular-expression-in-c-c

Stackoverflow does not allow to answer my own question. So I post it as a faked "question"

When I reading [C: A reference manual] Chapter 3: Prepressors. An idea emerges:

 #define STR(a) #a
 #define R(var, re)  static char var##_[] = STR(re);\
 const char * var = ( var##_[ sizeof(var##_) - 2] = '\0',  (var##_ + 1) );

 R(re, "\w\d");
 printf("Hello, world[%s]\n",  re);

It's portable in both C and C++, only uses standard preprocessing features. The trick is to use macro to expand \ inside liternal string and then remove the leading and tailing double quote strings.

Now I think it's the best way until C++0x really introduce the new literal string syntax R"...". And for C I think it'll be the best way for a long time.

The side effect is that we cannot defined such a variable in the global scope in C. Because there's a statement to remove the tailing double-quote character. In C++ it's OK.

A: 

This is not a question. If you want to add your own solution to your previous question, I suggest that you edit your original question with a section for your solution.

Kranu
And this should be a comment.
thyrgle
@thyrgle: touche
Kranu