How to break a line?

I have a very long line, I just want to extract some specific line inside this line. How can i do this?

For example, I have:

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

and yes, now I want to get a substring of this long string and only get http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg

Please show me how I can do this.

+3
source share
3 answers

You can use regular expressions for this:

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]]);
}

This regular expression is at the heart of the solution:

src="([^"]*)"

It matches the contents of the attribute srcand captures the contents between quotation marks (note the pair of parentheses). This header is then retrieved to [r rangeAtIndex:1]and used to retrieve the portion of the string you are looking for.

0
source

, , NSRegularExpression.

, , ( ):

- (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);
0

All Articles