RegEx用于解析html;

时间:2011-10-26 14:12:21

标签: objective-c ios regex facebook parsing

你能帮我制作RegExp吗?在大字符串中我需要找到这些子串(2种格式):

HTTP://www.facebook.com/profile.php ID = 100002234024152&安培; REF = FFA \ HTTP://www.facebook.com/alesya.yuldasheva REF = FFA \

我试过这些:

@"\\b(https?):\/\/www.facebook.com([.]{*})ref=ffa";
@"[{http:\/\/www.facebook.com}([.]{*}){ref=ffa}]";
@"[http:\/\/www.facebook.com]([.])*[ref=ffa]";

我使用RegexKitLite,其语法与普通目标c正则表达式相同。

3 个答案:

答案 0 :(得分:1)

NSError *error = nil;
NSString *htmlAdr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com"] encoding:NSUTF8StringEncoding error:&error];
if (error){
    NSLog(@"Yaz");
}
 // NSLog(@"%@", htmlAdr);
NSString *wereWeAreGoingToFind = htmlAdr;
NSString *whatWewAreGoingToFind1 = @"\\bhttps?:\\\\/\\\\/[a-zA-Z0-9\\-.]*\\\\/profile.php\\?id=([\\d]*)\\&ref=ffa";
NSString *whatWewAreGoingToFind = @"\\bhttps?:\\\\/\\\\/[a-zA-Z0-9\\-.]*\\\\/([a-zA-Z0-9\\-.]*)\\?ref=pb";
NSArray *matchArray = [wereWeAreGoingToFind componentsMatchedByRegex:whatWewAreGoingToFind capture:1L];
NSArray *matchArray1 = [wereWeAreGoingToFind componentsMatchedByRegex:whatWewAreGoingToFind1 capture:1L];
matchArray = [matchArray arrayByAddingObjectsFromArray:matchArray1];
NSLog(@"%@", matchArray);

答案 1 :(得分:0)

我知道没有Objective-c,但这个正则表达式是你想要的:

\b(https?)://www.facebook.com/(.*?)ref=ffa\b

不言而喻,你需要自己逃避斜线

答案 2 :(得分:0)

看起来你需要逃避'。'在网址中也是如此。例如:

@ “(?HTTPS):(?*)\ b // WWW \ .facebook \的.com / REF = FFA”;