Alexandre Ouicher
Alexandre Ouicher

Reputation: 856

Regex in Objective-c

I'm looking for all occurrences for strings in a text but i don't understand why code dosen't work. In the text, there are 2 occurences.

The Text :

<div class="infosLigneDetail pointer" onclick="javascript:toggleForfait('1');">
                    Alexandre OUICHER - Forfait Free illimité à 19,99 euros - 0627505460                        <input

The search string :

Alexandre OUICHER - Forfait Free illimité à 19,99 euros - 0627505460

The regex :

NSRegularExpression *regexpComptes = [NSRegularExpression regularExpressionWithPattern:@"(?<=javascript:toggleForfait('[0-9]');\">).*?(?=<input)" options:NSRegularExpressionSearch error:NULL];
NSArray *matchesComptes = [regexpComptes matchesInString:content
                                                   options:0
                                                     range:NSMakeRange(0, [content length])];

Do you know where is the problem ?

Upvotes: 0

Views: 185

Answers (1)

Jack
Jack

Reputation: 5768

You didn't escape the ( and ) in toggleForfait('[0-9]'). It's being treated as a capture group. It should be \( and \) respectively... toggleForfait\('[0-9]'\)

The regex you want is (?<=javascript:toggleForfait\('[0-9]'\);\">).*?(?=<input) with the dotall option enabled NSRegularExpressionDotMatchesLineSeparators

Look here: http://regexr.com?30ool

Upvotes: 2

Related Questions