-1

My code is below,but it doesn't work.

NSString *regex = @"^#";

NSPredicate *regextest = [NSPredicate
                          predicateWithFormat:@"SELF MATCHES %@", regex];

if ([regextest evaluateWithObject:secondHalfString] == YES) {
    NSLog(@"Match!");
} else {
    NSLog(@"No match!");
}

Anything wrong?

Monolo
  • 18,205
  • 17
  • 69
  • 103
Tom H
  • 517
  • 1
  • 6
  • 10

3 Answers3

1

matches in a predicate works on the whole string, so to obtain what you are after, write the regex like this:

@"^#.*"
Monolo
  • 18,205
  • 17
  • 69
  • 103
0

note that

The predicate format string syntax is different from the regular expression syntax.

on the Predicate documention.

for regex on cocoa look at this answer.

Community
  • 1
  • 1
Ria
  • 10,237
  • 3
  • 33
  • 60
0

If you are looking for a string that starts with a certain string you should use something like @"SELF BEGINSWITH #", it is much faster than using regular expressions.

hypercrypt
  • 15,389
  • 6
  • 48
  • 59