在斜杠之前删除字符串,目标C.

时间:2011-12-23 13:24:02

标签: objective-c regex cocoa

我在字符串中的正则表达式中需要帮助:

  

/folder1/folder2/folder3/folder4/folder5/folder6/folder7/ololo/ImageName.png

我需要匹配:

  

ololo / ImageName.png

你如何在Objective-C中做到这一点?

3 个答案:

答案 0 :(得分:1)

匹配字符串。

NSPredicate *predicate = nil;
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES 'ololo/ImageName.png'"];
if (![predicate evaluateWithObject:@"/folder1/folder2/folder3/folder4/folder5/folder6/folder7/ololo/ImageName.png"]) 
{
// do your work here.. 
}

答案 1 :(得分:0)

你可以将刺痛与此相匹配 -

if ([@"/folder1/folder2/folder3/folder4/folder5/folder6/folder7/ololo/ImageName.png" rangeOfString:@"ololo/ImageName.png"].location != NSNotFound) 
{
   //true
   // do your work here
}

答案 2 :(得分:0)

以下是使用正则表达式的方法:

NSString *path = @"quick/brown/fox/jumps.png";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^/]+[/][^/]+$" options:0 error:&error];
NSRange rangeOfMatch = [regex rangeOfFirstMatchInString:path options:0 range:NSMakeRange(0, path.length)];
NSLog(@"From %d, length %d", (int)rangeOfMatch.location, (int)rangeOfMatch.length);

此代码找到fox/jumps.png