无法使用NSRegularExpression(正则表达式模式)提取字符串-iPhone [英] Unable to extract string using NSRegularExpression (regex pattern) - iphone
本文介绍了无法使用NSRegularExpression(正则表达式模式)提取字符串-iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在[]括号之间提取字符串
I want to extract string between [ ] brackets
例如原始字符串:@这是一个测试[获取此字符串]."
for Example Original string: @"this is a test [to get this string]."
我想获取[]之间的提取字符串,即本例中的获取此字符串".
I want to get extract string between [] i.e "to get this string" in this example.
请在下面找到我的代码
NSMutableString *predicateString = [[NSMutableString alloc] initWithFormat:@"%@",@"this is a test [to get this string]."];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\[[^\].]*\]"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSMutableArray *rangeArray = [[NSMutableArray alloc] init];
__block NSUInteger count = 0;
[regex enumerateMatchesInString:predicateString options:0 range:NSMakeRange(0, [predicateString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
NSRange matchRange = [match range];
matchRange.length = matchRange.length+1;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSLog(@"matchRange.location: %d",matchRange.location);
NSLog(@"matchRange.length: %d",matchRange.length);
if (++count >= 100) *stop = YES;
}];
如果我做错了事,请告诉我.
Please let me know if I am doing something wrong.
谢谢.
推荐答案
看起来表达式是错误的,您需要转义斜线(例如: @"\\ [[^ \\].] * \\]"
).
Looks like the expression is wrong, you need to escape the slashes (eg : @"\\[[^\\].]*\\]"
).
这篇关于无法使用NSRegularExpression(正则表达式模式)提取字符串-iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文