3

私は非常に長い文字列を持っています。その文字列内の特定の文字列を抽出したいだけです。どうやってやるの?

たとえば、私は持っています:

this is the image <img src="http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg"> and it is very beautiful.

はい、今はこの長い文字列の部分文字列を取得して、http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg

これを行う方法を教えてください。

4

3 に答える 3

0

これには正規表現を使用できます。

NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"src=\"([^\"]*)\"" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *text = @"this is the image <img src=\"http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg\"> and it is very beautiful.";
NSArray *imgs = [regex matchesInString:text options:0 range:NSMakeRange(0, [text length])];
if (imgs.count != 0) {
    NSTextCheckingResult* r = [imgs objectAtIndex:0];
    NSLog(@"%@", [text substringWithRange:[r rangeAtIndex:1]]);
}

この正規表現がソリューションの核心です。

src="([^"]*)"

属性のコンテンツと一致しsrc、引用符の間のコンテンツをキャプチャします (括弧のペアに注意してください)。このキャプションは で取得され[r rangeAtIndex:1]、探している文字列の部分を抽出するために使用されます。

于 2012-05-08T10:40:02.710 に答える
0

おそらくNSRegularExpressionクラスを使用して、正規表現を使用する必要があります。

これは、あなたが望むことを正確に行う例です(hereから):

- (NSString *)stripOutHttp:(NSString *)httpLine
{
    // Setup an NSError object to catch any failures
    NSError *error = NULL;  
    // create the NSRegularExpression object and initialize it with a pattern
    // the pattern will match any http or https url, with option case insensitive
    NSRegularExpression *regex = [NSRegularExpression
        regularExpressionWithPattern:@"https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?" 
                             options:NSRegularExpressionCaseInsensitive
                               error:&error];
    // create an NSRange object using our regex object for the first match in the string httpline
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:httpLine
                                                         options:0
                                                           range:NSMakeRange(0, [httpLine length])];
    // check that our NSRange object is not equal to range of NSNotFound
    if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
    {
        // Since we know that we found a match, get the substring from the parent
        // string by using our NSRange object
        NSString *substringForFirstMatch = [httpLine substringWithRange:rangeOfFirstMatch];
        NSLog(@"Extracted URL: %@",substringForFirstMatch);
        // return the matching string
        return substringForFirstMatch;
    }

    return NULL;
}
于 2012-05-08T10:27:07.557 に答える
0
NSString *urlString = nil;
NSString *htmlString = //Your string;

NSScanner *scanner = [NSScanner scannerWithString:htmlString];

[scanner scanUpToString:@"<img" intoString:nil];
if (![scanner isAtEnd]) {
    [scanner scanUpToString:@"http" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@">"];
    [scanner scanUpToCharactersFromSet:charset intoString:&urlString];
}
NSLog(@"%@", urlString);
于 2012-05-08T10:30:36.590 に答える