2012-04-25 52 views
0

我正在寻找文本中所有出现的字符串,但我不明白为什么代码不起作用。在文中,有2次出现。正则表达式Objective-c

文本:

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

搜索字符串:

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

正则表达式:

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])]; 

你知道问题出在哪里?

+0

没有错误。字符串未找到 – 2012-04-25 00:05:31

回答

2

你没有逃过()toggleForfait('[0-9]')。它被视为一个捕获组。它应该是\(\)分别... toggleForfait\('[0-9]'\)

你想要的正则表达式是(?<=javascript:toggleForfait\('[0-9]'\);\">).*?(?=<input)与DOTALL启用选项NSRegularExpressionDotMatchesLineSeparators

看看这里:http://regexr.com?30ool

+0

谢谢。也感谢您链接到regexr.com。我知道空气中的工具,但不知道链接。 – 2012-04-26 09:33:07

+0

感谢NSRegularExpressionDotMatchesLineSeparators的dotall枚举值。 – diadyne 2013-01-31 20:57:19