简体   繁体   English

在Objective-C中可以获取正则表达式匹配在字符串中的位置

[英]In objective-c is it possible to get the position of a regex match within the string

If I have the string "Hello World", is it possible to use NSRegularExpression with the pattern @"World" to get the position of the match, ie in the "Hello World" example the position/index of the match should be "6"? 如果我有字符串“ Hello World”,是否可以将NSRegularExpression与@@ World模式一起使用来获取比赛的位置,即在“ Hello World”示例中,比赛的位置/索引应为“ 6” “?

in php I'd use preg_match with the "PREG_OFFSET_CAPTURE" flag to achieve this, does objective-c support this? 在PHP中,我将preg_match与“ PREG_OFFSET_CAPTURE”标志一起使用来实现此目标,objective-c是否支持此功能?

You can do it the Cocoa way: 您可以通过可可方式做到这一点:

NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"world" options:0 error:NULL];
// omitted error checking for the sake of simplicity
NSString *str = @"Hello world!";
[regex enumerateMatchesInString:str
    options:0
    range:NSMakeRange(0, str.length)
    usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
    {
        NSLog(@"Match at [%d, %d]", result.range.location, result.range.length);
    }];
[regex release];

Or the POSIX way (this may be convenient for you, since you want only one match, and this function/method returns the match range directly): 或POSIX方式(这可能对您来说很方便,因为您只需要一个匹配项,并且此函数/方法直接返回匹配范围):

#include <regex.h>

- (NSRange)matchString:(NSString *)string toRegex:(NSString *)regex
{
    regex_t regex_obj;
    regmatch_t match;
    const char *regex_str;
    const char *match_str;
    int error;

    regex_str = [regex UTF8String];
    error = regcomp(&regex_obj, regex_str, REG_EXTENDED);
    if (error)
    {
        return NSMakeRange(NSNotFound, 0);
    }

    match_str = [string UTF8String];
    error = regexec(&regex_obj, match_str, 1, &match, 0);
    if (error)
    {
        return NSMakeRange(NSNotFound, 0);
    }

    regfree(&regex_obj);
    return NSMakeRange(match.rm_so, match.rm_eo - match.rm_so);
}

This is somewhat long in Cocoa, but you can do it: 这在可可粉中有些长,但是您可以这样做:

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression
    regularExpressionWithPattern:@"world"
    options:NSRegularExpressionSearch
    error:&error];
NSString *str = @"Hello, world!";
NSTextCheckingResult *match = [regex firstMatchInString:str
    options:0
    range:NSMakeRange(0, [str length])];
if (match) {
    NSRange matchRange = [match range];
    NSLog(@"%lu", matchRange.location);
}

This prints 7 . 打印7

如果您打算大量使用RegEx,建议您查看RegexKit或RegexKitLite。

Yes it is possible. 对的,这是可能的。 You can use the NSRegularExpression method, rangeOfFirstMatchInString:options:range: which returns the range of the first match. 您可以使用NSRegularExpression方法rangeOfFirstMatchInString:options:range:返回第一个匹配项的范围。 You could also do this with the NSString method rangeOfString: if you don't need to use REGEX. 您也可以使用NSString方法rangeOfString来执行此操作:如果不需要使用REGEX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM