我正在寻找一个RegEx来从URL中提取链接。网址如下:
/redirecturl?u=http://www.abc.com/&tkn=Ue4uhv&ui=fWrQfyg46CADA&scr=SSTYQFjAA&mk=4D6GHGLfbQwETR
我需要从上面的网址中提取链接http://www.abc.com
。
我尝试了RegEx:
redirecturl\\?u=(?<link>[^\"]+)&
这样可行,但问题是在第一次出现&amp;之后它不会截断所有字符。
如果您可以修改RegEx以便我获得链接,那就太棒了。
提前致谢。
答案 0 :(得分:2)
redirecturl\\?u=([^\"&]+)
当它到达&
或者根本没有&
时应截断
答案 1 :(得分:0)
如何使用URI class?
示例:
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"
答案 2 :(得分:0)
通过\来逃避特殊字符,即匹配/使用 [\ /]
var matchedString = Regex.Match(s,@"[\/]redirecturl[\?]u[\=](?<link>.*)[\/]").Groups["link"];
答案 3 :(得分: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. [[\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();