Reputation: 31
I'm looking for a RegEx to extract links from a URL. The URL would be as below:
/redirecturl?u=http://www.abc.com/&tkn=Ue4uhv&ui=fWrQfyg46CADA&scr=SSTYQFjAA&mk=4D6GHGLfbQwETR
I need to extract the link http://www.abc.com
from the above URL.
I tried the RegEx:
redirecturl\\?u=(?<link>[^\"]+)&
This works, but the problem is that it does not truncate all the characters after the first occurrence of &.
It would be great if you could modify the RegEx so that I just get the link.
Thanks in advance.
Upvotes: 3
Views: 593
Reputation: 1246
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();
Upvotes: 0
Reputation: 4764
Escape the special characters by \ i.e for matching / use [\/]
var matchedString = Regex.Match(s,@"[\/]redirecturl[\?]u[\=](?<link>.*)[\/]").Groups["link"];
Upvotes: 0
Reputation: 57210
What about using URI class ?
Example:
string toParse = "/redirecturl?u=http://www.abc.com/&tkn=Ue4uhv&ui=fWrQfyg46CADA&scr=SSTYQFjAA&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"
Upvotes: 0
Reputation: 9172
redirecturl\\?u=([^\"&]+)
That should truncate when it reaches an &
or if there is no &
at all
Upvotes: 2