RegEx для извлечения ссылки - PullRequest
       0

RegEx для извлечения ссылки

3 голосов
/ 27 декабря 2010

Я ищу RegEx для извлечения ссылок из URL.URL будет выглядеть следующим образом:

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

Мне нужно извлечь ссылку http://www.abc.com из указанного выше URL.

Я попробовал RegEx:

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

Это работает, но проблема в том, что он не усекает все символы после первого появления &.

Это будетбыло бы здорово, если бы вы могли изменить RegEx так, чтобы я просто получил ссылку.

Заранее спасибо.

Ответы [ 4 ]

2 голосов
/ 27 декабря 2010
redirecturl\\?u=([^\"&]+)

Это должно усекаться, когда оно достигает & или если & нет вообще

0 голосов
/ 18 января 2011
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. [[\w@][\w.:@]+]
//      [\w@][\w.:@]+
//          Any character in this class: [\w@]
//          Any character in this class: [\w.:@], one or more repetitions
//  Literal /, zero or one repetitions
//  Any character in this class: [\w\.?=%&=\-@/$,], any number of repetitions

public Regex MyRegex = new Regex(
      "(?<Protocol>\\w+):\\/\\/(?<Domain>[\\w@][\\w.:@]+)\\/?[\\w\\."+
      "?=%&=\\-@/$,]*",
    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();
0 голосов
/ 27 декабря 2010

Выход из специальных символов на \ i.e для сопоставления / использования [\ /]

var matchedString = Regex.Match(s,@"[\/]redirecturl[\?]u[\=](?<link>.*)[\/]").Groups["link"];
0 голосов
/ 27 декабря 2010

Как насчет использования URI-класса ?

Пример:

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"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...