I tried to check matches facebook url and get profile in one regular expression: I have:
http://www.facebook.com/profile.php?id=123456789
https://facebook.com/someusername
I need:
123456789
someusername
using this regular expression:
(?<=(https?://(www.)?facebook.com/(profile.php?id=)?))([^/#?]+)
I get:
profile.php
someusername
Whats wrong?
I advise you to use the System.Uri
class to get this information. It does the difficult work for you and can handle all sorts of edge cases.
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"
I agree with others on using System.Uri but your regex needs two modifications to work:
(\n|$) at the end
(?<=(https?://(www\.)?facebook\.com/(profile\.php\?id=)?))([^/#?]+)(\n|$)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With