How to get the file extension from a url?

后端 未结 5 942
小蘑菇
小蘑菇 2021-02-03 20:02

New to ruby, how would I get the file extension from a url like:

http://www.example.com/asdf123.gif

Also, how would I format this string, in c#

5条回答
  •  野性不改
    2021-02-03 20:27

    You could use Ruby's URI class like this to get the fragment of the URI (i.e. the relative path of the file) and split it at the last occurrence of a dot (this will also work if the URL contains a query part):

    require 'uri'
    your_url = 'http://www.example.com/asdf123.gif'
    fragment = URI.split(your_url)[5]
    
    extension = fragment.match(/\.([\w+-]+)$/)
    

提交回复
热议问题