Objc Regex - 用组解析字符串
Objc Regex - Parse string with groups
我正在尝试解析“:”前后的文本文件和组信息,这样我就可以知道什么与什么相关。
我正在使用以下
//NSString *pattern = @"\[(.*?)\]"; //[]
//NSString *pattern = @"(\w+:)"; //word:
NSString *pattern =@"(\w+:)\[(.*?)\]";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSString *input = contents; //contents being the value taken from the text file
NSMutableArray *matches = [NSMutableArray arrayWithCapacity:[myArray count]];
for (NSTextCheckingResult *match in myArray) {
//NSUInteger numberOfRanges = [match numberOfRanges];
//NSLog(@"%lu", (unsigned long)numberOfRanges);
NSRange matchRange = [match rangeAtIndex:1];
[matches addObject:[input substringWithRange:matchRange]];
NSLog(@"%@", [matches lastObject]);
}
“[]”有效,"word:" 有效,但当我将它们结合在一起时,我什么也得不到。
括号(输出)
2016-03-26 09:20:36.302 LevelBuilder[14658:550234] 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1
...
2016-03-26 09:20:36.304 LevelBuilder[14658:550234] 17,1
字(输出)
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] tiles:
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] boxes:
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] goals:
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] start:
我在模式中遗漏了什么
NSString *pattern =@"(\w+:)\[(.*?)\]";
数据为例
tiles:
[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1]
,[0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,1,0,0,1]
,[1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,1]
,[1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1]
,[1,0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,1]
,[1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1]
,[1,0,0,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,1]
,[1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1]
,[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
],
boxes: [
[5,6],[6,5],[10,5],[11,5],[16,4],[16,5],[16,6],[16,7]
],
goals: [
[1,8],[1,9],[1,10],[1,11],[2,10],[2,11],[3,10],[3,11]
],
start: [17,1]
由于您需要匹配每个 "word" 后跟相应的 [...]
,您可以使用包含 2 个捕获组的正则表达式模式和一个前瞻性 "temper" 惰性点匹配图案:
@"(?s)(\w+):\s*\[(.*?)\](?=,\r?\n\s*\w+:|$)"
模式匹配:
(?s)
- 使 .
匹配换行符
(\w+):
- 匹配并捕获到第 1 组一个或多个字母数字字符,然后匹配 :
\s*
- 0+ 个空白字符
\[(.*?)\]
- 匹配 [...]
如果最后 ]
...
(?=,\r?\n\s*\w+:|$)
- 后跟字符串结尾 ($
) 或逗号后跟换行符后跟 0+ 空格符号后跟 "word" 后跟 :
.
我正在尝试解析“:”前后的文本文件和组信息,这样我就可以知道什么与什么相关。
我正在使用以下
//NSString *pattern = @"\[(.*?)\]"; //[]
//NSString *pattern = @"(\w+:)"; //word:
NSString *pattern =@"(\w+:)\[(.*?)\]";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSString *input = contents; //contents being the value taken from the text file
NSMutableArray *matches = [NSMutableArray arrayWithCapacity:[myArray count]];
for (NSTextCheckingResult *match in myArray) {
//NSUInteger numberOfRanges = [match numberOfRanges];
//NSLog(@"%lu", (unsigned long)numberOfRanges);
NSRange matchRange = [match rangeAtIndex:1];
[matches addObject:[input substringWithRange:matchRange]];
NSLog(@"%@", [matches lastObject]);
}
“[]”有效,"word:" 有效,但当我将它们结合在一起时,我什么也得不到。
括号(输出)
2016-03-26 09:20:36.302 LevelBuilder[14658:550234] 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1
...
2016-03-26 09:20:36.304 LevelBuilder[14658:550234] 17,1
字(输出)
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] tiles:
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] boxes:
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] goals:
2016-03-26 09:18:18.189 LevelBuilder[14464:543898] start:
我在模式中遗漏了什么
NSString *pattern =@"(\w+:)\[(.*?)\]";
数据为例
tiles:
[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1]
,[0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,1,0,0,1]
,[1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,1]
,[1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1]
,[1,0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,1]
,[1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1]
,[1,0,0,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,1]
,[1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1]
,[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
],
boxes: [
[5,6],[6,5],[10,5],[11,5],[16,4],[16,5],[16,6],[16,7]
],
goals: [
[1,8],[1,9],[1,10],[1,11],[2,10],[2,11],[3,10],[3,11]
],
start: [17,1]
由于您需要匹配每个 "word" 后跟相应的 [...]
,您可以使用包含 2 个捕获组的正则表达式模式和一个前瞻性 "temper" 惰性点匹配图案:
@"(?s)(\w+):\s*\[(.*?)\](?=,\r?\n\s*\w+:|$)"
模式匹配:
(?s)
- 使.
匹配换行符(\w+):
- 匹配并捕获到第 1 组一个或多个字母数字字符,然后匹配:
\s*
- 0+ 个空白字符\[(.*?)\]
- 匹配[...]
如果最后]
...(?=,\r?\n\s*\w+:|$)
- 后跟字符串结尾 ($
) 或逗号后跟换行符后跟 0+ 空格符号后跟 "word" 后跟:
.