4

Is there a way using NSRegularExpression to specify that you want to do a case-sensitive search? I am trying to match the upper-case TAG "ACL" in the text below. The pattern I am using is simply:

// Pattern
[A-Z]+

// SearchText
<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>

// Code:
NSString *textBuffer = @"<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>";
NSString *pattern = @"([A-Z]+)";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSTextCheckingResult *result = [regExp firstMatchInString:textBuffer options:0 range:NSMakeRange(0, [textBuffer length])];
NSLog(@"OBJECT CLASS: %@", [textBuffer substringWithRange:[result range]]);

Output: (with case-Insensative I am getting the first "td" as expected, when what I really want is "ACL"

I know that NSRegularExpressionCaseInsensitive is wrong, I was hoping there would be a NSRegularExpressionCaseSensitive. Also there is a flagOption ?(i) that also specifies a case-insensitive search but again nothing for case-sensative. What am I missing?

Arun_
  • 1,806
  • 2
  • 20
  • 40
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294

1 Answers1

12

Case sensitive is the default. Dont put the insensitive flag in there.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
wattson12
  • 11,176
  • 2
  • 32
  • 34
  • 1
    Thank you, it does work if you replace NSRegularExpressionCaseInsensitive with 0, just seems strange that there is not a dedicated option for it. – fuzzygoat Feb 08 '12 at 17:12
  • @fuzzygoat There's no such option because case-sensitive search, being faster, is the default option. – Costique Feb 08 '12 at 17:45
  • @fuzzygoat example in Swift: `NSRegularExpression(pattern: "^@[a-z]+", options: NSRegularExpressionOptions(rawValue: 0))` – Bartłomiej Semańczyk Jan 05 '16 at 11:47