Get the domain name of the URL in Java or IP and port get IP and domain name method one, use regular expressions
Publicstatic string GetIP (string url) {Filter using regular expressions, String re =((HTTP|FTP|HTTPS)://) (([a-za-z0-9._-]+) | ( [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3})) (([a-za-z]{2,6}) | (: [0-9]{1,4})? "; Stringstr ="";Compile regular expression pattern pattern = pattern.compile (re);//ignoring case notation //Pattern pat = Pattern.compile (RegEx, pattern.case_insensitive); Matcher Matcher = pattern.matcher (URL); //if url==http://127.0.0.1:9040 or www.baidu.com, regular expressions indicate matching if (Matcher.matches ()) {str = URL;} else {string[] split2 = Url.split (re); if (Split2.length > 1) {String substring = url.substring ( Span class= "Hljs-number" >0, Url.length ()-Split2[1].length ()); str = substring;} else {str = Split2[0];}} return STR;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
Get the IP and domain name method two, using the Java tool class implementation, call Mode GetIP (Uri.create ("http://127.0.0.1:9040/1/1.html");
private static uri getip (Uri uri) {uri Effectiveuri = nu ll try {//URI (string scheme, string userInfo, string host, int port , string //path, string query,string fragment) Effectiveuri = New URI (Uri.getscheme (), Uri.getuserinfo (), Uri.gethost (), Uri.getport (), null, null, null); } catch (Throwable var4) {Effectiveuri = null;} return Effectiveuri; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
Test
System. Out. println (GetIP ("Http://www.baidu.com/system/verList")); System. Out. println (GetIP ("Http://WWW.BAIDU.COM/TESE/verList")); System. Out. println (GetIP ("http://blog.csdn.net/weasleyqi/article/details/7912647")); System. Out. println (GetIP ("https://segmentfault.com/q/1010000000703645")); System. Out. println (GetIP ("Http://www.cnblogs.com/afarmer/archive/2011/08/29/2158860.html")); System.out.println (GetIP (; System.out.println ( " 1: "+ getip (" http://127.0.0.1:9040/system/verList ")) .out.println ( " 2: "+ getip (" http://127.0.0.1:9040/system/verList?loginName=1&password= ad07fb25aa2d3a9f96ee12f25e0be902 ")) ; System.out.println ( " 3: "+ getip (" http://127.0.0.1:9040/"))
Results
Summarize
It is recommended to use the second method, after all, is the Java tool class provided, matching complete.
The first, suitable for simple domain names, advantages: light, one way to achieve.
Get the URL's domain name or IP and port in Java