用于解析 html 的正则表达式;

发布于 2024-12-12 01:55:00 字数 419 浏览 0 评论 0原文

你能帮我制作正则表达式吗?在大字符串中,我需要找到这些子字符串(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,其语法与正常的 Objective C 正则表达式相同。

Could you help me to make RegExp? In large string I need to find these substrings(2 formats):

http://www.facebook.com/profile.php?id=100002234024152&ref=ffa\
http://www.facebook.com/alesya.yuldasheva?ref=ffa\

I tried these:

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

I use RegexKitLite, which syntax same as normal objective c regular expressions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必在意 2024-12-19 01:55:00
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);
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);
呆萌少年 2024-12-19 01:55:00

我不知道 Objective-C,但这个正则表达式正是您想要的:

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

不用说,您需要自己转义斜杠

I know no objective-c, but this regular expression is what you want:

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

It goes without saying that you'll need to escape the slashes yourself

逆夏时光 2024-12-19 01:55:00

看来您需要转义“.”也在网址中。例如:

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

It looks like you need to escape the '.' in the url too. For example:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文