adit
adit

Reputation: 33674

NSRegularExpression not matching

So I have a string:

users/9881570/?access_token=

that I try to match with the regex:

 NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"users/\\d/?access_token=" options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* wordArray = [regex matchesInString:self.currentRequestURL_ 
                                            options:0 range:NSMakeRange(0, [self.currentRequestURL_ length])];

However, the wordArray has a count of 0. Why is this not matching?

Upvotes: 2

Views: 1026

Answers (2)

borrrden
borrrden

Reputation: 33423

For one thing, you need to escape the question mark, and for another you need a plus sign (+) after your \d to indicate 1 or more numbers. As it is now you only look for one digit.

@"users/\\d+/\\?access_token="

Upvotes: 2

Tom Dalling
Tom Dalling

Reputation: 24145

Because the question mark is a special character in regular expressions, and it needs to be escaped.

Upvotes: 0

Related Questions