从字符串中删除动态子字符串

时间:2014-10-11 09:55:46

标签: objective-c

如果我有随机字符串,我需要删除这样的子字符串:

" fsdfsd / p90x95 / fsdfsdf" >>> " fsdfsd / fsdfsdf"

" fsdfsd / s110x72 / fsdfsdsdff" >>> " fsdfsd / fsdfsdsdff"

" / sdfsdfd / fsdfsd / t800x1024 / fsdfsdsdff" >>> " / sdfsdfd / fsdfsd / fsdfsdsdff"

我需要每次都删除子字符串,当只修复(规则)是: ' /'可选的字母数字' x'数字' /'

如何在所有情况下检测此子字符串并将其删除?

1 个答案:

答案 0 :(得分:2)

使用正则表达式/\w+x\d+

NSString *str = @"/sdfsdfd/fsdfsd/t800x1024/fsdfsdsdff";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/\\w+x\\d+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, [str length]) withTemplate:@""];
NSLog(@"%@", modifiedString); // output: /sdfsdfd/fsdfsd/fsdfsdsdff
相关问题