我尝试检查匹配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)
怎么了?
我建议你用这个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)
我同意其他人使用System.Uri但你的正则表达式需要两个修改工作:
(\n | $)最后
(????<=(HTTPS://(WWW \)的Facebook \的.com /(配置文件\ .PHP\ID =)))([?^ /#] +)(\n | $)