4

In my code I make extensive use of regular expression, and my patterns look something like this

regexp = [[NSRegularExpression alloc] initWithPattern:@".*?\\\+.*?\\\+.*?\\\+.*?\\\+.*?\\\+.*?\\\+" options:0 error:nil];

For the escape sequences I get compiler warnings like "Unknown escape sequence +" which is extremely annoying because it is not wrong in my case. How can I get rid of this warning?

toom
  • 12,864
  • 27
  • 89
  • 128
  • 1
    There is no way except to disable all warnings (or use an escape sequence that doesnt cause a compiler warning). – chown Nov 22 '11 at 20:19
  • @chown This is not true. See Pragma warning push/pop syntax http://stackoverflow.com/questions/4193476/is-using-pragma-warning-push-pop-the-right-way-to-temporarily-alter-warning-lev – griotspeak Apr 07 '13 at 22:21

1 Answers1

10

You have to many back-slash characters, use:

regexp = [[NSRegularExpression alloc] initWithPattern:@".*?\\+.*?\\+.*?\\+.*?\\+.*?\\+.*?\\+" options:0 error:nil];

To get a \ in a string it needs to be escaped: \\. So \\\+ applies the first \ to escaping the second \ and the third \ tries to escape the plus sign + which is an illegal escape.

zaph
  • 111,848
  • 21
  • 189
  • 228