Imgur ID match using Regex in ASP.NET C#
I am pretty inexperienced when it comes to regular expressions and wondered if anyone can help me achieve the follow开发者_如何学编程ing.
I need a regular expression that will validate if a certain URL is a valid imgur image and return the ID of the image.
Match imgurMatch = imgurRegex.Match(URL);
if(imgurMatch.Success)
id = imgurMatch.Groups[0].Value
Here are some examples:
http://imgur.com/gallery/qtPdb (ID = qtPdb)
http://i.imgur.com/RcVIa.jpg (ID = RcVIa)
(Could be of type .jpg, .png, .gif)
http://imgur.com/3ZZuG (ID = 3ZZuG)
I think a regular expression that can handle the above and return the correct ID would be good enough for me, since even if validation fails for some reason, I will be able to handle it in another way.
Please let me know if any more details is needed.
Thanks!
Tribe84
Regex imgurRegex=new Regex(@"http://(?:i\.imgur\.com/(?<id>.*?)\.(?:jpg|png|gif)|imgur\.com/(?:gallery/)?(?<id>.*))$");
Match imgurMatch = imgurRegex.Match(URL);
if(imgurMatch.Success)
id = imgurMatch.Groups["id"].Value
精彩评论