2012-02-13 48 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:方法在这个方法中,你会得到的结果适合 你的正则表达式,你可以手动去除开始部分和问号,如果你想。

相关问题