iOS/iPhone:从正则表达式中提取组(NSRegularExpression)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS/iPhone:从正则表达式中提取组(NSRegularExpression)相关的知识,希望对你有一定的参考价值。

Extract groups from a Regular Expression
  1. NSRegularExpression* exp = [NSRegularExpression regularExpressionWithPattern:@"(put)-(expression)-(here)" options:NSRegularExpressionSearch error:&error];
  2.  
  3. if (error) {
  4. NSLog(@"%@", error);
  5. } else {
  6.  
  7. NSTextCheckingResult* result = [exp firstMatchInString:testString options:0 range:NSMakeRange(0, [productDescription length] ) ];
  8.  
  9. if (result) {
  10.  
  11. NSRange groupOne = [result rangeAtIndex:1]; // 0 is the WHOLE string.
  12. NSRange groupTwo = [result rangeAtIndex:2];
  13. NSRange groupThree = [result rangeAtIndex:3];
  14.  
  15. NSLog( [testString substringWithRange:groupOne] );
  16. NSLog( [testString substringWithRange:groupTwo] );
  17. NSLog( [testString substringWithRange:groupThree] );
  18. }
  19. }

以上是关于iOS/iPhone:从正则表达式中提取组(NSRegularExpression)的主要内容,如果未能解决你的问题,请参考以下文章

使用正则表达式从 URL 中提取***域和二级域

正则表达式 c# 获取捕获组的子组

正则表达式 1. 分组提取/非捕获组

如何从 Perl 中的一行中提取非空白组?

在 PySpark 中提取多个正则表达式匹配项

Python学习手册之正则表达式示例--邮箱地址提取