可能重复:
retrieving the substring based on condition from a string in iphone
Identifying the string in between two strings in iphone
我是iphone的新手。我有一点疑问,就是我有很多字符串(接近66),比如格式为href =“/ mp3 / KJV / 21_Ecc / Ecc_KJV.zip”在单个字符串中,但所有这些字符串的不同之处在于更改名称Ecc的地方但是所有都是相同的格式。所以,我的请求是如何使用正则表达式获取所有以(href =“)开头并以(.zip)结尾的字符串以及如何为其编写正则表达式上面的格式字符串,用于标识该格式的所有字符串。
答案 0 :(得分:0)
请试试这个。希望这可以帮到你。感谢
NSString * param = nil;
NSString * objString = [[NSString alloc] initWithString:@“href = \”/ mp3 / KJV / 21_Ecc / Ecc_KJV.zip \“”];
NSRange start = [objString rangeOfString:@"href="];
if (start.location != NSNotFound)
{
param = [objString substringFromIndex:start.location + start.length];
NSRange end = [param rangeOfString:@".zip"];
if (end.location != NSNotFound)
{
param = [objString substringToIndex:end.location];
}
}
NSLog(@"Substring==%@",param);
//param now contains your value (or nil if not found)
答案 1 :(得分:0)
NSString *sourceString = ...// your string for search substrings
NSString *pattern = @"href=(.*?).zip";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSArray *results = [regex matchesInString:sourceString options:0 range:NSMakeRange(0, [sourceString length])];
for (NSTextCheckingResult* result in results)
{
NSString *resultString = [sourceString substringWithRange:result.range];
NSLog(@"%@",resultString);
}