2012-02-13 46 views
3

」結尾的單詞我有一個包含網頁的html代碼的NSString 現在,我如何搜索以字符串開頭的所有單詞'data-src =「http://'並以」? 「結尾我將它們保存在一個數組中 我的字符串被稱爲urlPage在NSString中搜索以特定文本開頭並以「

PS:我不希望單詞具有'data- SRC =。「HTTP://」和」我只是想這2

+0

你不明白我想做什麼? – 2012-02-13 22:43:35

回答

1

下面是一些示例代碼:

NSString *string; 
NSString *pattern; 
NSRegularExpression *regex; 

string = @" aa data-src=\"http://test1\" cd data-src=\"http://test2\" cd"; 
pattern = @"data-src=\"http://([^\"]+)\""; 

regex = [NSRegularExpression 
     regularExpressionWithPattern:pattern 
     options:NSRegularExpressionCaseInsensitive 
     error:nil]; 

NSArray *matches = [regex matchesInString:string 
            options:0 
            range:NSMakeRange(0, [string length])]; 

for (NSTextCheckingResult *match in matches) { 
    NSRange range = [match rangeAtIndex:1]; 
    NSLog(@"match: '%@'", [string substringWithRange:range]); 
} 

的NSLog輸出:
匹配: 'TEST1'
比賽: 'TEST2'

+0

非常感謝你!這真的幫助了我! – 2012-02-13 23:15:25

0

最好的辦法是使用NSRegularExpression之間的字搜索字符串尤其是enumerateMatchesInString:options:range:usingBlock:方法在這個方法中,你會得到的結果適合 你的正則表達式,你可以手動去除開始部分和問號,如果你想。

相關問題