正则表达式匹配问题

时间:2012-12-13 15:02:22

标签: c# regex

我有一个字符串

  

<a href="/makeuppro/video?st.cmd=altGroupVideoAll&amp;st.groupId=oqxdtikenuenvnwuj0rxiwhgvyuvhjhzjrd&amp;st.directLink=on&amp;st.referenceName=makeuppro&amp;st._aid=NavMenu_AltGroup_Video"

我需要获取groupID oqxdtikenuenvnwuj0rxiwhgvyuvhjhzjrd

我试过

string groupId = Regex.Match(content, @"altGroupVideoAll&amp;st.groupId=(?<id>[^""]+)&amp").Groups["id"].Value;

但结果是:

oizrximcmbsyyvjxacd0rpkkmgxwuvhinnuvczz&amp;st.directLink=on&amp;st.referenceName=makeuppro

为什么,正确的正则表达式是什么?

3 个答案:

答案 0 :(得分:4)

您需要使用不情愿的量词来停留在第一个&amp: -

@"altGroupVideoAll&amp;st.groupId=(?<id>[^""]+?)&amp"

答案 1 :(得分:0)

试试这个:

groupId=(?<id>[^&]+)

我怀疑id不会包含&amp;字符。您的原始正则表达式是贪婪的,并尝试匹配可能的最长字符串。

答案 2 :(得分:0)

Hy @ user1895750和@Jared Harley,

你混淆了Lazy和Greedy Expression,请参阅下面的代码。

    /// <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&amp;st.groupId=oqxdtikenuenvnwuj0rxiwhgvyuvhjhzjrd&amp;st.directLink=on&amp;st.referenceName=makeuppro&amp;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 ("&amp;st.directLink=on&amp;st.referenceName=makeuppro"), because the last "&amp" is after "makeuppro" .
        // Here the correct pattern.
        var pattern = "groupId=(?<id>[^\"\"]+?)&amp";

        // 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;
    }

我希望它可以帮到你!

此致