开发者

Imgur ID match using Regex in ASP.NET C#

开发者 https://www.devze.com 2023-03-28 02:27 出处:网络
I am pretty inexperienced when it comes to regular expressions and wondered if anyone can help me achieve the follow开发者_如何学编程ing.

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  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号