none
Regex pattern help or Loop? RRS feed

  • Question

  • How would make a pattern for Regex for this.. I know that the numbers would be...   \d

    <div class="c-model-group js-model-group ">
      <div class="c-model-group__numbers">
                      
      <span class="c-model js-model c-model--outline">
        1
          </span>
                  
      <span class="c-model js-model c-model--outline">
        2
          </span>
                  
      <span class="c-model js-model c-model--outline">
        3
          </span>
                                  </div>
    </div>

    --------------------------------------------------------------------------------------------------------------

    Or how could you make this code below loop threw and find all matches the data string and just keep adding to 1 string like  

    list += d

    Tried with no success...

    public static string getBetween(string strSource, string strStart, string strEnd)
    {
        int Start, End;
        if (strSource.Contains(strStart) && strSource.Contains(strEnd))
        {
            Start = strSource.IndexOf(strStart, 0) + strStart.Length;
            End = strSource.IndexOf(strEnd, Start);
            return strSource.Substring(Start, End - Start);
        }
        else
        {
            return "";
        }
    }

    Friday, December 7, 2018 5:32 PM

Answers

  • Wow ok.. Um well lets start..  I was still working on a solution to the problem and I solved it .. Oh and I tested your ridiculously simple solution and it grabbed a lot of wrong data.. (Epic Fail....)

    This is what works...

      public static string getBetween(string strSource, string strStart, string strEnd)
            {
                int Start, End;
                string Temp = strSource;
                string List;

                // Data Filter =========================

                if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                {
                    Start = Temp.IndexOf(strStart, 0) + strStart.Length;
                    End = Temp.IndexOf(strEnd, Start);
                    List = Temp.Substring(Start, End - Start);
                    Temp = Temp.Remove(Start, End - Start);

                    //--------------------------------------------------------------

                    if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                    {
                        Start = Temp.IndexOf(strStart, End) + strStart.Length;
                        End = Temp.IndexOf(strEnd, Start);
                        List += Temp.Substring(Start, End - Start);
                        Temp = Temp.Remove(Start, End - Start);
                        Temp = List;
                    }

                    //--------------------------------------------------------------

                    if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                    {
                        Start = Temp.IndexOf(strStart, End) + strStart.Length;
                        End = Temp.IndexOf(strEnd, Start);
                        List += Temp.Substring(Start, End - Start);
                        Temp = Temp.Remove(Start, End - Start);
                        Temp = List;
                    }

                    //--------------------------------------------------------------

                    if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                    {
                        Start = Temp.IndexOf(strStart, End) + strStart.Length;
                        End = Temp.IndexOf(strEnd, Start);
                        List += Temp.Substring(Start, End - Start);
                        Temp = Temp.Remove(Start, End - Start);
                        Temp = List;
                    }

                    return Temp;
                }

                else
                {
                    return "";
                }    
            }

    -------------------------------------------------------------------------------------------------

         string List = getBetween(Data, "-3 data-header=\"Result", "</div>");

         List = Regex.Replace(List, "[^.0-9]", "");

           if (List.Length == 6)
               {
                  //First num
                     num = List.Substring(0, 3);
                     myList.Add(num);

                    //Second num
                       num = List.Substring(3, 3);
                       myList.Add(num);
                 }

    What this does is it fines the location of the match... grab the data between the range.. from that end range search again get that match .. merge to the same string.. send it back for non number filtering ... since i know that the number are in the right order and i know their length i separate them and add to a list...

    I can never get over this GOD complex that some people have, and most of the not even close.. I consider my self a humble guy .... whats with all the trying to put people down thing?? Wasted time and Energy.. Look back in History the smartest people were humble and mellow... 

    Well I guess I will go get something to eat because I'm ridiculously hungry.. LOL

    • Marked as answer by superlurker Sunday, December 9, 2018 12:04 AM
    Sunday, December 9, 2018 12:04 AM

All replies

  • The simple fact of the matter is that regular expressions are just not the right solution for parsing HTML.  It's far too easy for the originator to make trivial changes that look the same on screen but break your expression.

    The right answer is to use an HTML parser.  The Html Agility Pack looks like it has a good history, https://html-agility-pack.net/?z=codeplex.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, December 7, 2018 7:17 PM
  • How would make a pattern for Regex for this.. I know that the numbers would be...   \d

    <div class="c-model-group js-model-group ">
      <div class="c-model-group__numbers">
                      
      <span class="c-model js-model c-model--outline">
        1
          </span>
                  
      <span class="c-model js-model c-model--outline">
        2
          </span>
                  
      <span class="c-model js-model c-model--outline">
        3
          </span>
                                  </div>
    </div>

    --------------------------------------------------------------------------------------------------------------

    Or how could you make this code below loop threw and find all matches the data string and just keep adding to 1 string like  

    list += d

    Tried with no success...

    public static string getBetween(string strSource, string strStart, string strEnd)
    {
        int Start, End;
        if (strSource.Contains(strStart) && strSource.Contains(strEnd))
        {
            Start = strSource.IndexOf(strStart, 0) + strStart.Length;
            End = strSource.IndexOf(strEnd, Start);
            return strSource.Substring(Start, End - Start);
        }
        else
        {
            return "";
        }
    }

    Notice, Regex are based on patterns!

    Without pattern, Regex is not useful.

    You posted a bunch of tags, but you did not posted the pattern!

    We cannot guess what is the correct pattern, but as you posted 2 divs and 3 spans inside them, I will assume that the whole thing is a (repetitive) pattern. This way this, the solution using Regex is ridiculously simple:

                string inp=@"
    <div class=""c-model-group js-model-group "">
      <div class=""c-model-group__numbers"">
        <span class=""c-model js-model c-model--outline"">
          1
        </span>
        <span class=""c-model js-model c-model--outline"">
          2
        </span>
        <span class=""c-model js-model c-model--outline"">
          3
        </span>
      </div>
    </div>";
    

    With the input above, the ridiculously simple Regex solution is:

    Regex
        .Matches
        (
            Regex.Replace(Regex.Replace(inp,"\n",""),@">\s+<","><")
            ,@"(?<=<span[^>]+>)(.+?)(?=</span>)"
        ).OfType<Match>()
        .ToList().ForEach
        (
            m=>Console.WriteLine(m.Groups[1].ToString().Trim())
        );
    


    Saturday, December 8, 2018 8:05 AM
  • @Andrew Painter, aka MR BS,

    Do you see how simple it is thru Regex? It's a matter of having a pattern, dude!

    P.S.: if you don't like the way I treat you, ...  (I forgot the remaining...)


    • Edited by ritehere44 Saturday, December 8, 2018 8:08 AM
    Saturday, December 8, 2018 8:07 AM
  • Wow ok.. Um well lets start..  I was still working on a solution to the problem and I solved it .. Oh and I tested your ridiculously simple solution and it grabbed a lot of wrong data.. (Epic Fail....)

    This is what works...

      public static string getBetween(string strSource, string strStart, string strEnd)
            {
                int Start, End;
                string Temp = strSource;
                string List;

                // Data Filter =========================

                if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                {
                    Start = Temp.IndexOf(strStart, 0) + strStart.Length;
                    End = Temp.IndexOf(strEnd, Start);
                    List = Temp.Substring(Start, End - Start);
                    Temp = Temp.Remove(Start, End - Start);

                    //--------------------------------------------------------------

                    if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                    {
                        Start = Temp.IndexOf(strStart, End) + strStart.Length;
                        End = Temp.IndexOf(strEnd, Start);
                        List += Temp.Substring(Start, End - Start);
                        Temp = Temp.Remove(Start, End - Start);
                        Temp = List;
                    }

                    //--------------------------------------------------------------

                    if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                    {
                        Start = Temp.IndexOf(strStart, End) + strStart.Length;
                        End = Temp.IndexOf(strEnd, Start);
                        List += Temp.Substring(Start, End - Start);
                        Temp = Temp.Remove(Start, End - Start);
                        Temp = List;
                    }

                    //--------------------------------------------------------------

                    if (Temp.Contains(strStart) && Temp.Contains(strEnd))
                    {
                        Start = Temp.IndexOf(strStart, End) + strStart.Length;
                        End = Temp.IndexOf(strEnd, Start);
                        List += Temp.Substring(Start, End - Start);
                        Temp = Temp.Remove(Start, End - Start);
                        Temp = List;
                    }

                    return Temp;
                }

                else
                {
                    return "";
                }    
            }

    -------------------------------------------------------------------------------------------------

         string List = getBetween(Data, "-3 data-header=\"Result", "</div>");

         List = Regex.Replace(List, "[^.0-9]", "");

           if (List.Length == 6)
               {
                  //First num
                     num = List.Substring(0, 3);
                     myList.Add(num);

                    //Second num
                       num = List.Substring(3, 3);
                       myList.Add(num);
                 }

    What this does is it fines the location of the match... grab the data between the range.. from that end range search again get that match .. merge to the same string.. send it back for non number filtering ... since i know that the number are in the right order and i know their length i separate them and add to a list...

    I can never get over this GOD complex that some people have, and most of the not even close.. I consider my self a humble guy .... whats with all the trying to put people down thing?? Wasted time and Energy.. Look back in History the smartest people were humble and mellow... 

    Well I guess I will go get something to eat because I'm ridiculously hungry.. LOL

    • Marked as answer by superlurker Sunday, December 9, 2018 12:04 AM
    Sunday, December 9, 2018 12:04 AM