none
Regex problem RRS feed

  • Question

  • For <a href="article.jsp?id=38123&amp;typeid=57">

    I want to get "38123" and "57" from it. I'm very new to regex. please help me.

    Sunday, November 19, 2017 3:34 PM

Answers

  • You can try using following code

    string pattern = "\d+"; 
    string input = "<a href='article.jsp?id=38123&amp;typeid=57' >"; 
    foreach (Match m in Regex.Matches(input, pattern)) 
    {
    MessageBox.Show(m.Match);
    } 


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Sunday, November 19, 2017 4:23 PM
  • I assume that the "&amp;" is supposed to be just an ampersand.

    Note that for most processing of HTML it is better to not use regular expressions. However for something as simple as in this question the HTML DOM is too much if it is all you need to do. I would not use a RegEx for something so simple. The following is a possibility.

    const string FirstPart = "<a href=\"article.jsp?id=";
    string s = "<a href=\"article.jsp?id=38123&typeid=57\">";
    if (!s.StartsWith(FirstPart))
    {
        Console.WriteLine("Does not start with " + FirstPart);
        return;
    }
    s = s.Substring(FirstPart.Length);
    int p = s.IndexOf('&');
    if (p > 0)
        Console.WriteLine(s.Substring(0, p));
    



    Sam Hobbs
    SimpleSamples.Info

    Sunday, November 19, 2017 11:54 PM
  • Hello CSharp_Language,

    The following is a simple regex demo that using group and capture .

    string s= "<a href='article.jsp?id=38123&amp;typeid=57'>";
    
                var input = "typeid";
             
                foreach (Match m in Regex.Matches(s, String.Format(@"(?<={0}=)\d+(?=)", input)))
                {
                    Console.WriteLine(m.Value);
                }
    

    For more information about groups, you could see here.

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#zerowidth_positive_lookahead_assertion

    Sincerely,

    Fei Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 20, 2017 3:00 AM
    Moderator

All replies

  • You can try using following code

    string pattern = "\d+"; 
    string input = "<a href='article.jsp?id=38123&amp;typeid=57' >"; 
    foreach (Match m in Regex.Matches(input, pattern)) 
    {
    MessageBox.Show(m.Match);
    } 


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    Sunday, November 19, 2017 4:23 PM
  • I assume that the "&amp;" is supposed to be just an ampersand.

    Note that for most processing of HTML it is better to not use regular expressions. However for something as simple as in this question the HTML DOM is too much if it is all you need to do. I would not use a RegEx for something so simple. The following is a possibility.

    const string FirstPart = "<a href=\"article.jsp?id=";
    string s = "<a href=\"article.jsp?id=38123&typeid=57\">";
    if (!s.StartsWith(FirstPart))
    {
        Console.WriteLine("Does not start with " + FirstPart);
        return;
    }
    s = s.Substring(FirstPart.Length);
    int p = s.IndexOf('&');
    if (p > 0)
        Console.WriteLine(s.Substring(0, p));
    



    Sam Hobbs
    SimpleSamples.Info

    Sunday, November 19, 2017 11:54 PM
  • Hello CSharp_Language,

    The following is a simple regex demo that using group and capture .

    string s= "<a href='article.jsp?id=38123&amp;typeid=57'>";
    
                var input = "typeid";
             
                foreach (Match m in Regex.Matches(s, String.Format(@"(?<={0}=)\d+(?=)", input)))
                {
                    Console.WriteLine(m.Value);
                }
    

    For more information about groups, you could see here.

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#zerowidth_positive_lookahead_assertion

    Sincerely,

    Fei Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 20, 2017 3:00 AM
    Moderator