嗨@user1895750 和@Jared Harley,
您混淆了惰性和贪婪表达式,请参见下面的代码。
/// <summary>
/// Example for how to extract the group Id.
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
private static string ExtractNumber(string xml)
{
// Extracted number.
string groupId = string.Empty;
// Input text
xml = @"<a href=""/makeuppro/video?st.cmd=altGroupVideoAll&st.groupId=oqxdtikenuenvnwuj0rxiwhgvyuvhjhzjrd&st.directLink=on&st.referenceName=makeuppro&st._aid=NavMenu_AltGroup_Video""";
// Here is the key, you have to use "?" after "(?<id>[^\"\"]+"
// This is called "Lazy expression", and it is different from the "Greedy expression".
// Lazy expression uses the "?", like ".*?\r". So it will match the expression until they find the first carriage return (\r).
// If you use ".*\r" (Greedy Expression), it will match until they find the last carriage return of the input. Thats why you matched ("&st.directLink=on&st.referenceName=makeuppro"), because the last "&" is after "makeuppro" .
// Here the correct pattern.
var pattern = "groupId=(?<id>[^\"\"]+?)&";
// Match the desired part of the input.
var match = Regex.Match(xml, pattern);
// Verify the match sucess.
if (match.Success)
{
// Finally, use the group value to isolate desired value.
groupId = match.Groups["id"].Value;
}
return groupId;
}
我希望它对你有帮助!
真挚地,