-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular.m
More file actions
31 lines (29 loc) · 1.25 KB
/
Copy pathregular.m
File metadata and controls
31 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// main.m
// homework09_16
//
// Created by 古玉彬 on 15/9/16.
// Copyright (c) 2015年 guyubin. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//需要匹配的语句
NSString *str1 = @"[00:01:01]where are you from[00:01:07]i come from china";
//正则表达式
NSString *pattern = @"\\d{2}\\][^\\[]*";
NSMutableArray *array = [[NSMutableArray alloc] init];
//转成oc语法
NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *arr3 = [regular matchesInString:str1 options:0 range:NSMakeRange(0, str1.length)];
for(NSTextCheckingResult *res in arr3){
//返回的是符合条件的范围。(res.range)
NSMutableString *m_str1 = [[NSMutableString alloc ]initWithString:[str1 substringWithRange:res.range]];
[m_str1 replaceOccurrencesOfString:@"]" withString:@": " options:0 range:NSMakeRange(0, [m_str1 length])];
[array addObject:m_str1];
}
NSString *resultString = [array componentsJoinedByString:@" "];
NSLog(@"%@",resultString);
}
return 0;
}