Objective c 从字符串中删除动态子字符串
如果我有随机字符串,我需要删除如下子字符串: “fsdfsd/p90x95/fsdfsdf”>>>“fsdfsd/fsdfsdf” “fsdfsd/s110x72/FSDFSDSF”>>“fsdfsd/FSDFSDSF” “/sdfsdfd/fsdfsd/t800x1024/fsdfsdsf”>>>”/sdfsdfd/fsdfsd/fsdfsdsf” 我每次都需要删除子字符串,当唯一固定的(规则)是: “/”可选字母编号“x”编号“/”Objective c 从字符串中删除动态子字符串,objective-c,Objective C,如果我有随机字符串,我需要删除如下子字符串: “fsdfsd/p90x95/fsdfsdf”>>>“fsdfsd/fsdfsdf” “fsdfsd/s110x72/FSDFSDSF”>>“fsdfsd/FSDFSDSF” “/sdfsdfd/fsdfsd/t800x1024/fsdfsdsf”>>>”/sdfsdfd/fsdfsd/fsdfsdsf” 我每次都需要删除子字符串,当唯一固定的(规则)是: “/”可选字母编号“x”编号“/” 如何检测此子字符串并在所有情况下删除它?使用正则表达式,/
如何检测此子字符串并在所有情况下删除它?使用正则表达式,
/\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