17

I'm getting a weird warning, and as a result my regex search isn't working. Here's the line:

NSRange r = [HTML rangeOfString:@"\|(.*)\|" options:NSRegularExpressionSearch];

Where HTML is a string that I'm sure contains a single match for the above regex.

The warning is only on the first occurrence of "\|", not on both.

Any help is much appreciated!

Mason
  • 6,345
  • 15
  • 67
  • 113

2 Answers2

56

You're getting the warning because \| is not a valid escape sequence in Objective-C (or C or C++ for that matter). The compiler is ignoring that and just using a raw | character instead, so the string you're actually passing in is @"|(.*)|".

To get the behavior you want, you have to escape the backslash in your source code so that the regex engine sees the literal backslash and interprets the | character as a literal instead of as alternation, e.g. @"\\|(.*)\\|".

Adam Rosenfield
  • 360,316
  • 93
  • 484
  • 571
  • Follow up question: Let's say I was matching that to a string like "|adsfsad| asdfsadf |" and I only want to catch the first one ("|asdfsad|") instead of it matching the entire string, how can I specify that? – Mason Jul 13 '12 at 19:08
  • You can use "\\|(.*?)\\|" to catch the first one ("|asdfsad|") – Wu Zhou May 10 '13 at 05:13
0

Just to add up, if you are dealing with special character sequences in unicode format, you can use something like this:

const unichar specialCharSequence='some special character';
if(specialCharSequence==L'\uxxxx')
{
   //handle the occurence of this special character
}