gpt4 book ai didi

iphone - NSRegularExpression 提取文本

转载 作者:行者123 更新时间:2023-12-03 21:19:02 25 4
gpt4 key购买 nike

全部,

我有一本包含两个键和值的字典。我需要从它们中提取一些片段并将它们放入单独的字符串中。

{
IP = "192.168.17.1";
desc = "VUWI-VUWI-ABC_Dry_Cleaning-R12-01";
}

这就是我调用 description 时字典的样子。

我希望新的输出是这样的:

NSString *IP = @"192.168.17.1";
NSString *desc = @"ABC Dry Cleaning"; //note: I need to get rid of the underscores
NSString *type = @"R";
NSString *num = @"12";
NSString *ident = @"01";

我如何实现这一目标?

我已经阅读了有关 NSRegularExpression 的 Apple 开发人员文档,但我发现它很难理解。我确信一旦我在这里得到一些帮助,我将来就能弄清楚,我只需要开始。

提前致谢。

最佳答案

好吧,首先,您必须获取与每个键关联的对象:

NSString *ip = [dic objectForKey:@"IP"]; //Btw, you shouldn't start a variable's name with a capital letter.
NSString *tempDesc = [dic objectForKey:@"desc"];

然后,我要做的就是根据字符 - 拆分 tempDesc 中的字符串。

NSArray *tmpArray = [tempDesc componentsSeparatedByString:@"-"];

然后您只需获取您感兴趣的字符串或子字符串,并根据需要重新格式化它们:

NSString *desc = [[tmpArray objectAtIndex:2] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
NSString *type = [[tmpArray objectAtIndex:3] substringToIndex:1];
NSString *num = [[tmpArray objectAtIndex:3] substringFromIndex:1];
NSString *ident = [tmpArray objectAtIndex:4];

如您所见,无需使用 NSRegularExpression 即可完美运行。

关于iphone - NSRegularExpression 提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6791259/

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