2013-04-05 74 views
1

我想在C#中定義一個正則表達式,當給定一個url時,將返回除協議之外的url,例如http。也就是說,我需要跳過任何協議,只返回域和路徑。正則表達式跳過協議url

任何想法?

回答

1

使用這個表達式(?<=://).+

使用這個表達式://(.+)沒有回顧後,值[1]

2

試着用以下的正則表達式:在regexGroup

^[a-z]+://(.*)$ 


string input = "http://www.google.com/search"; 

Match match = Regex.Match(input, @"^[a-z]+://(.*)$", RegexOptions.IgnoreCase); 

if (match.Success) 
{ 
    string url = match.Groups[1].Value; 
} 
+0

THX,該訣竅。 – fsl 2013-04-05 10:50:05

+0

net.tcp:// localhost:808 – 2013-04-05 10:51:09

+0

'^ [az。] +://(。*)$' – hsz 2013-04-05 10:51:54

0

你可以使用簡單替換

string url = "http://url.com"; 
     if(url.Contains("http://")){ 
      url = url.Replace("http://",""); 
     } 
     else if(url.Contains("https://")){ 
      url = url.Replace("https://",""); 
     } 

url = Regex.Replace(url,@"[a-z]+://","");