我对正则表达式很陌生,我正在努力学习它。
这是我的字符串:
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
我想把它拆分成一个看起来像这样的数组:
@[@"Mozzila", @"4.0", @"compatible", @"MSIE 5.0", @"Windows NT", @"DigExt"];
这是我尝试过的代码:
NSString *expression = @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSRegularExpression *testExpression = [NSRegularExpression regularExpressionWithPattern: @"([a-zA-Z]+)/([1-9.]+) \(([a-z]+); ([a-zA-Z .]+); ([a-zA-Z ]+); ([a-zA-Z]+)\)" options:NSRegularExpressionCaseInsensitive error:nil];
options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [testExpression matchesInString:expression
options:0
range:NSMakeRange(0, [expression length])];
NSLog(@"%@",matches);
还试过:
[testExpression enumerateMatchesInString:expression
options:0
range:NSMakeRange(0, [expression length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(@"Value: %@", [expression substringWithRange:[result rangeAtIndex:1]]);
}];
还有:
NSRegularExpression *testExpression = [NSRegularExpression
regularExpressionWithPattern: @"(\w+)/(\w+) \((\w+);([\w .]+); ([\w ]+); (\w+)\)" options:NSRegularExpressionCaseInsensitive
error:nil];
但是日志是空的。我做错了什么?
答案 0 :(得分:6)
NSString *expression = @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSRegularExpression *testExpression = [NSRegularExpression regularExpressionWithPattern:@"(.+)/([0-9\\.]+) \\(([^)]*).*"
options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [testExpression matchesInString:expression
options:0
range:NSMakeRange(0, [expression length])];
NSLog(@"%@",matches);
NSMutableArray *array = [@[] mutableCopy];
[matches enumerateObjectsUsingBlock:^(NSTextCheckingResult *obj, NSUInteger idx, BOOL *stop) {
for (int i = 1; i< [obj numberOfRanges]; ++i) {
NSRange range = [obj rangeAtIndex:i];
NSString *string = [expression substringWithRange:range];
if ([string rangeOfString:@";"].location == NSNotFound) {
[array addObject: string];
} else {
NSArray *a = [string componentsSeparatedByString:@";"];
for (NSString *s in a) {
[array addObject: [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}
}
}
}];
array
包含
<__NSArrayM 0x10010d540>(
Mozilla,
4.0,
compatible,
MSIE 5.0,
Windows NT,
DigExt
)
@"(.+)/([0-9\\.]+) \\(([^)]*).*"
^__^ capture group 1
^_________^ capture group 2
^ the char (
^_____^ capture group 3
\\
来逃避点,否则它将再次代表任何角色。 \\(
表示会跟随(
,但由于我们没有将它包含在我们的捕获组中,因此我们并不关心它。 ([^)]*)
说“任何可打印的,但不是)
现在我们用它们的范围迭代捕获组。我们从索引1开始,因为索引0将给出完整表达式的范围
([1-9.]+)
这与0
不匹配,而且这些点代表任何可打印字符。你想要
([0-9\\.]+)
答案 1 :(得分:1)
NSString *yourStr= @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSArray *arrComponents = [yourStr componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/;()"]];
答案 2 :(得分:0)
我认为你应该更好地使用下面这样的: -
NSString *yourString = @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSArray *array = [yourString componentsSeparatedByString:@";"];
NSlog(@"%@",array);
就像你希望split
的任何格式一样,你可以将特殊字符传递给componentsSeparatedByString
方法