这么简单但是我很挣扎,我每两年左右做一次RegExp,所以我生锈了
我有这两个url字符串 http://localhost:58876/Products/Product1 https://localhost:58876/Products/Product1
我想要的结果是
本地主机:58876
基本上删除了http(s)://以及第一个/之后的所有内容我最终得到了带或不带端口号的域
P.S:我正在使用C#
答案 0 :(得分:1)
这对我有用(测试了int notepad ++):
(\w+:\d+)
答案 1 :(得分:0)
只需匹配斜杠即可:/^https?:\/\/([^\/]+)\/.*$/
var url = 'http://localhost:58876/Products/Product1';
var match = url.match(/^https?:\/\/([^\/]+)\/.*$/);
if(match&&match.length>0)document.write(match[1]);

更短:/\/\/([^\/]+)/
。请注意,有很多(更多)更好的方法来解析URL。根据您的平台,有PHP的parse_url
,NodeJS的url
module或像uri.js这样的库来处理有效URI的多个面。
答案 2 :(得分:0)
您可以使用以下正则表达式来拆分网址:
((HTTP [S] | FTP):/?)???/([^:/ \ S] +)(:([^ /] ))((/ \ W +) /)([\瓦特 - ] + [?^#\ S] +)(\([^#] )?(。#()?))
RegEx职位3和5是您正在寻找的职位。
答案 3 :(得分:0)
(^[^h]|\/\/)([\w\d\:\@\.]+:?[\d]?+)
然后在c#:
string address = ...
char[] MyChar = {'/'};
string NewString = address.TrimStart(MyChar);
编辑:也使用localhost:58876 / Products / Product1 !