2012-05-08 5 views
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

が、私はこれを行うことができますどのように私を示してください取得したいです。

答えて

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]で取得され、探している文字列の一部を抽出するために使用されます。

+0

ありがとう、これは私が欲しいものです。 – user1035877

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; 
} 
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); 
関連する問題