gpt4 book ai didi

iphone - 无法使用 NSRegularExpression(正则表达式模式)提取字符串 - iphone

转载 作者:行者123 更新时间:2023-12-03 20:09:29 25 4
gpt4 key购买 nike

我想提取[]括号之间的字符串

例如原始字符串:@“这是一个测试[获取此字符串]。”

我想在 [] 之间获取提取字符串,即本例中的“获取此字符串”。

请在下面找到我的代码

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;

}];

如果我做错了什么,请告诉我。

谢谢。

最佳答案

看起来表达式是错误的,您需要转义斜杠(例如:@"\\[[^\\].]*\\]")。

关于iphone - 无法使用 NSRegularExpression(正则表达式模式)提取字符串 - iphone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com