开发者

Question about regular expression evaluation using RegexKitLite

I am trying to get all css link from html like this segment of code:

<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/default.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/datepicker.css"/>
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/carousel.css" rel="stylesheet" type="text/css" />
<link href="http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/langoverlay_en-us.css" rel="stylesheet" type="text/css" />

Here is my code:

-(void)matchCSS:(NSString *)html{
    NSString *regexString = @"href=\".*\.css\"";
    NSArray *matchArray = NULL;
    matchArray = [html componentsMatchedByRegex:regexString];
    NSLog(@"matchArray: %@", matchArray);
}

However, what I got is a little bit crazy:

"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/default.css\" rel=\"stylesheet\" type=\"text/css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/datepicker.css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/carousel.css\" rel=\"style开发者_运维百科sheet\" type=\"text/css\"",
"href=\"http://media.ticketmaster.com/en-us/css/1c84b57773d8f594407f0b0b78d67aba/tm/langoverlay_en-us.css\" rel=\"stylesheet\" type=\"text/css\""

These are not pure link, some of them contains some other tags that I don't want. I didn't see anything wrong with my RE. Any suggestion?


The problem is with the .*, which is too greedy. You should match every character that is not the quote character. I am not familiar with the regular expression syntax used by RegexKitLite, but I think the regular expression should be something like @"href=\"[^\"]*\\.css\"".

You should probably use a group; in that way, the function would return you only the characters included in the group, and not all the characters matching the regular expression. If I am not wrong, the regular expression should be something like @"href=\"([^\"]*\\.css)\"", in this case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜