2012-04-25 54 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