[erlang-questions] Parsing Url.

Allen Kim <>
Mon Sep 26 16:48:47 CEST 2011


For those who is interested in parsing url with a single function,

-record(url,{protocol,user,password,host,port,path}).

parse_url(Url) ->
    {ok,RE}= re:compile("^(http[s]?://)?([a-z0-9]+(:[^@]+)?@)?([a-z0-9\.\-]+(:[0-9]+)?)([/].*)?$",[caseless]),
    case re:run(Url,  RE, [global, {capture, all, list}]) of
        {match,[[_Match,Scheme,UserPass,Password,Host]]} ->
            Protocol = re:replace(Scheme,"://","",[{return,list}]),
            User = re:replace(UserPass,Password++"@","",[{return,list}]),
            #url{protocol=Protocol,user=User,password=Password,host=Host,port="80",path=""};
        {match,[[_Match,Scheme,UserPass,Password,UrlHost,UrlPort]]} ->
            Protocol = re:replace(Scheme,"://","",[{return,list}]),
            User = re:replace(UserPass,Password++"@","",[{return,list}]),
            Host = re:replace(UrlHost,":[0-9]+$","",[{return,list}]),
            Port = re:replace(UrlPort,":","",[{return,list}]),
            #url{protocol=Protocol,user=User,password=Password,host=Host,port=Port,path=""};
        {match,[[_Match,Scheme,UserPass,Password,UrlHost,UrlPort,Path]]} ->
            Protocol = re:replace(Scheme,"://","",[{return,list}]),
            User = re:replace(UserPass,Password++"@","",[{return,list}]),
            Host = re:replace(UrlHost,":[0-9]+$","",[{return,list}]),
            Port = re:replace(UrlPort,":","",[{return,list}]),
            #url{protocol=Protocol,user=User,password=Password,host=Host,port=Port,path=Path};
        nomatch ->
            #url{}
    end.


Allen Kim



More information about the erlang-questions mailing list