目标C regex如何把最后一行放入数组 [英] objective C regex how to put the last line into array

查看:99
本文介绍了目标C regex如何把最后一行放入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个示例行如下在目标c中编写正则表达式以捕获在{}大括号到数组中的最后英语含义,尽管它们可以超过3个意义,即4个大括号或5



龟737D U9f9c B213 S18 S16 XJ03535 XH2147 XN5445 V7106 H2150 O2598A MN48847 MP12.1151 P2-2-16 I2n9.1 Q2711.7 ZPP4-18-4 Ygui1 Yjun1 Yqiu1 Wgwi Wgu Wgyunキュウ你可以使用捕获括号来捕获一个或多个元素之间的所有内容,例如,你可以使用捕获的圆括号来捕获所有的元素。

  NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@[{]([^} *)[}]options:0 error:& error]; 
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0,[string length])usingBlock:^(NSTextCheckingResult * result,NSMatchingFlags flags,BOOL * stop){
NSRange range = [result rangeAtIndex: 1];
NSString * found = [string substringWithRange:range];
NSLog(@%@,found);
}];


if I have a sample line as below how to write regex in objective c to capture the last english meanings which enclosed in {} braces into array though they can be more than 3 meanings ie 4 braces or 5

龜 737D U9f9c B213 S18 S16 XJ03535 XH2147 XN5445 V7106 H2150 O2598A MN48847 MP12.1151 P2-2-16 I2n9.1 Q2711.7 ZPP4-18-4 Ygui1 Yjun1 Yqiu1 Wgwi Wgu Wgyun キ キュウ キン かめ T1 ひさし {turtle} {tortoise} {or}

解决方案

You can use the capturing parentheses to capture everything in between the braces, but not including the braces:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[{]([^}]*)[}]" options:0 error:&error];
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSRange range = [result rangeAtIndex:1];
    NSString *found = [string substringWithRange:range];
    NSLog(@"%@", found);
}];

这篇关于目标C regex如何把最后一行放入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆