Nikolay Dyankov
Nikolay Dyankov

Reputation: 7244

Objective C Regex?

I'm trying to parse a 7-digit number from a page's source code and the pattern that I look for is

/nnnnnnn"

where "n" is a digit. I'm trying with the following regex and in a regex test site it works, but not in obj-c. Is it possible that I'm passing the wrong option or something?

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/\d\d\d\d\d\d\d\">" options:NSRegularExpressionSearch error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:contents
                                                    options:0
                                                      range:NSMakeRange(0, [contents length])];

Upvotes: 1

Views: 147

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

You should double the backslashes in front of your ds, like this:

@"/\\d\\d\\d\\d\\d\\d\\d\">"

Backslash is a special character inside a string literal: the character after it is interpreted differently. In order for the regex engine to see a backslash, you need two slashes in the literal.

Upvotes: 1

Related Questions