如何使用正则表达式在iOS中获取匹配项? [英] How to get matches in iOS using regular expression?

查看:306
本文介绍了如何使用正则表达式在iOS中获取匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似'stackoverflow.html'的字符串,并且在正则表达式'stack(.).html'中我想要在(.)中具有该值.

I got a string like 'stackoverflow.html' and in the regular expression 'stack(.).html' I would like to have the value in (.).

我只能找到像这样的NSPredicate:

I could only find NSPredicate like:

NSString    *string     = @"stackoverflow.html";
NSString    *expression = @"stack(.*).html";
NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", expression];
BOOL match = [predicate evaluateWithObject:string]

但是当我使用NSRegularExpression时,这只能告诉我有一个匹配项并且没有返回字符串:

But that only tells I got a match and doesn't return a string, when I use NSRegularExpression:

NSRange range = [string rangeOfString:expression options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location == NSNotFound) return nil;

NSLog (@"%@", [string substringWithRange:(NSRange){range.location, range.length}]);

它将给我返回的总字符串为stackoverflow.html,但是我只对(.*)中的内容感兴趣.我想重新获得溢价".在PHP中,这很容易做到,但是如何在iOS的xCode中实现呢?

It will give me the total string back, stackoverflow.html, but I am only interested in that whats in (.*). I want to get 'overflow' back. In PHP this was easy to do, but how can you accomplish this in xCode for iOS?

如果我愿意的话,在逻辑上:

Logically if I would do this:

NSInteger firstPartLength  = 5;
NSInteger secondPartLength = 5;
NSLog (@"%@", [string substringWithRange:(NSRange){range.location + firstPartLength, range.length - (firstPartLength + secondPartLength)}]

它给我正确的结果溢出".但是问题是,在许多情况下,我不知道第一部分或第二部分的长度.那么有什么方法可以获取应该在(.*)中的值?

It gives me the propert result 'overflow'. But the problem is in many cases I dont know the length of the first part or the second part. So is there a way I can get the value that should be in (.*) ?

还是我必须决定通过找到(.)的位置来选择最丑陋的方法,然后从那里计算出第一部分和第二部分?但是在正则表达式中,您可能也有([a-z]),但是使用另一个正则表达式来获取()之间的值的位置然后再利用它来计算左右部分的丑陋方式呢?如果我还有更多的话会发生什么?像"A(.)应该找到(.*)的答案."我想要一个数组,其结果为[0]等于A之后的值,[1]等于to之后的值.

Or must I decide to choose the ugliest method by finding the location of (.) and calculate the first and second part from there? But in regular expression you might also have ([a-z]) but then the ugly way of using another regular expression to get the location of values between () and then use that to calculate the left and right part? And what happends if I have more? like 'A (.) should find the answer to (.*).' I would like to have an array as result with values [0] the value after A and [1] the value after to.

我希望我的问题很清楚.

I hope my question is clear.

预先感谢

推荐答案

在iOS 4.0+中,您可以使用NSRegularExpression:

in iOS 4.0+, you can use NSRegularExpression:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"stack(.*).html" options:0 error:NULL];
NSString *str = @"stackoverflow.html";
NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
// [match rangeAtIndex:1] gives the range of the group in parentheses
// [str substringWithRange:[match rangeAtIndex:1]] gives the first captured group in this example

这篇关于如何使用正则表达式在iOS中获取匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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