2010-12-27 3 views
3

Je suis à la recherche d'un RegEx pour extraire les liens d'une URL. L'URL serait comme ci-dessous:RegEx pour extraire le lien

/redirecturl?u=http://www.abc.com/&tkn=Ue4uhv&ui=fWrQfyg46CADA&scr=SSTYQFjAA&mk=4D6GHGLfbQwETR 

J'ai besoin d'extraire le lien http://www.abc.com de l'URL ci-dessus.

J'ai essayé le RegEx:

redirecturl\\?u=(?<link>[^\"]+)& 

Cela fonctionne, mais le problème est qu'il ne tronque pas tous les caractères après la première apparition de &.

Ce serait génial si vous pouviez modifier le RegEx de sorte que je viens d'obtenir le lien.

Merci d'avance.

+1

[Ce] (http://stackoverflow.com/q/287144/82449) pourrait aider. –

Répondre

2
redirecturl\\?u=([^\"&]+) 

Cela devrait tronquer quand il atteint un & ou s'il n'y a pas du tout &

+0

Et prenez le deuxième groupe dans la collection de match. – Ikaso

0

Qu'en est-il en utilisant URI class?

Exemple:

string toParse = "/redirecturl?u=http://www.abc.com/&amp;tkn=Ue4uhv&amp;ui=fWrQfyg46CADA&amp;scr=SSTYQFjAA&amp;mk=4D6GHGLfbQwETR"; 

// remove "/redirecturl?u=" 
string urlString = toParse.Substring(15,toParse.Length - 15); 

var url = new Uri(urlString); 
var leftPart = url.GetLeftPart(UriPartial.Scheme | UriPartial.Authority); 
// leftPart = "http://www.abc.com" 
0

échapper les caractères spéciaux \ ie pour adapter/utiliser [\ /]

var matchedString = Regex.Match(s,@"[\/]redirecturl[\?]u[\=](?<link>.*)[\/]").Groups["link"]; 
0
using System.Text.RegularExpressions; 

// A description of the regular expression: 
// 
// [Protocol]: A named capture group. [\w+] 
//  Alphanumeric, one or more repetitions 
// :\/\/ 
//  : 
//  Literal/
//  Literal/
// [Domain]: A named capture group. [[\[email protected]][\w.:@]+] 
//  [\[email protected]][\w.:@]+ 
//   Any character in this class: [\[email protected]] 
//   Any character in this class: [\w.:@], one or more repetitions 
// Literal /, zero or one repetitions 
// Any character in this class: [\w\.?=%&=\[email protected]/$,], any number of repetitions 

public Regex MyRegex = new Regex(
     "(?<Protocol>\\w+):\\/\\/(?<Domain>[\\[email protected]][\\w.:@]+)\\/?[\\w\\."+ 
     "?=%&=\\[email protected]/$,]*", 
    RegexOptions.IgnoreCase 
    | RegexOptions.CultureInvariant 
    | RegexOptions.IgnorePatternWhitespace 
    | RegexOptions.Compiled 
    ); 


// Replace the matched text in the InputText using the replacement pattern 
string result = MyRegex.Replace(InputText,MyRegexReplace); 

// Split the InputText wherever the regex matches 
string[] results = MyRegex.Split(InputText); 

// Capture the first Match, if any, in the InputText 
Match m = MyRegex.Match(InputText); 

// Capture all Matches in the InputText 
MatchCollection ms = MyRegex.Matches(InputText); 

// Test to see if there is a match in the InputText 
bool IsMatch = MyRegex.IsMatch(InputText); 

// Get the names of all the named and numbered capture groups 
string[] GroupNames = MyRegex.GetGroupNames(); 

// Get the numbers of all the named and numbered capture groups 
int[] GroupNumbers = MyRegex.GetGroupNumbers();