Regexp to get domain from URL

雨燕双飞 提交于 2019-12-23 01:12:19

问题


Anyone knows the Regexp for AS3 to get the domain name from URL? E.g: "http://www.google.com/translate" will get "http://www.google.com"


回答1:


There's is a very complete utility class for dealing with URIs in as3corelib. Perhaps you might want to use this instead of rolling your own.

import com.adobe.net.URI;
var uri:URI = new URI("http://www.google.com/translate");
trace(uri.authority); // traces www.google.com



回答2:


This should do the work: http(s?)://([\w]+\.){1}([\w]+\.?)+ You can try this in GSkinner RegExr




回答3:


The previous regex provided missed some url types i needed, so for the benefit of googlers, I use

(http://|https://)?(www.)?[A-z]*(.com|.co.uk|.us|.org|.net|.mobi)

in C#

var regexMatch = Regex.Match(input, @"(http://|https://)?(www.)?[A-z]*(.com|.co.uk|.us|.org|.net|.mobi)", RegexOptions.Multiline);

string domain = regexMatch.Value;

See example at RegExr.

You can remove remove "(...)?" parts to stop that part appearing the match, for example to remove the http matching that I require.

Probably not perfect, but works for me. This site is an excellent reference for building up expressions.




回答4:


Try this one:

.*\/\/([^\/:]+).*

Surrounds the regexp by the preferred separators, which might depend on language. This diagram explain how it works.



来源:https://stackoverflow.com/questions/3431523/regexp-to-get-domain-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!