正则表达式获取Facebook个人资料

max*_*ins 7 c# regex

我尝试检查匹配facebook url并在一个正则表达式中获取配置文件:我有:

http://www.facebook.com/profile.php?id=123456789
https://facebook.com/someusername

我需要:

123456789
someusername
Run Code Online (Sandbox Code Playgroud)

使用这个正则表达式:

(?<=(https?://(www.)?facebook.com/(profile.php?id=)?))([^/#?]+)
Run Code Online (Sandbox Code Playgroud)

我明白了:

profile.php
someusername
Run Code Online (Sandbox Code Playgroud)

怎么了?

Rik*_*Rik 8

我建议你用这个System.Uri班来获取这些信息.它为您完成了艰巨的工作,可以处理各种边缘情况.

var profileUri = new Uri(@"http://www.facebook.com/profile.php?id=123456789");
var usernameUri = new Uri(@"https://facebook.com/someusername");

Console.Out.WriteLine(profileUri.Query); // prints "?id=123456789"
Console.Out.WriteLine(usernameUri.AbsolutePath); // prints "/someusername"
Run Code Online (Sandbox Code Playgroud)


nim*_*ima 5

我同意其他人使用System.Uri但你的正则表达式需要两个修改工作:

  • \ in(profile.php \?id =)
  • (\n | $)最后

    (????<=(HTTPS://(WWW \)的Facebook \的.com /(配置文件\ .PHP\ID =)))([?^ /#] +)(\n | $)