1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help sorting HTML code

Discussion in 'C, C++, C#' started by mrwho, Apr 2, 2010.

  1. mrwho

    mrwho Junior Member

    Joined:
    Apr 3, 2008
    Messages:
    122
    Likes Received:
    39
    Just now started to learn C# using Visual C# 2008 Express edition and having the hardest time trying to sort a HTML page and find the text i'm looking for.
    the text the program looks for looks like this: "[RandomUsername] selling 190 points for $49,000,000 per point"
    a wildcard would need to be used in some places so i can grab the right/whole string and there are multiple strings on the page that are similar so I would need it to only grab the 1st one it encounters.
    I have this coded in VBScript but now need to code it in C#

    here's the code in VBS:
    Code:
    Dim exchangera
    exchangera = ExchangeRate(0)
    For i = 1 To Len(exchangera)
    strPos = InStr(1, "$", Mid(exchangera, i, 1))
    If strPos > 0 Then
    strResult = Mid(exchangera, i, InStr(i + 1, exchangera, "p") - i)
    End If
    Next
    
    Dim strResult
    Dim CleanedString
    Dim i
    CleanedString =Null
    strAlphaNumeric = "0123456789"
    For i = 1 To Len(strResult)
    strNew = Mid(strResult,i,1)
    If instr(strAlphaNumeric,strNew) Then
    CleanedString = CleanedString & strNew
    CleanedString = Clng(CleanedString)
    End If
    Next
    The code in C# of course if sorted is gonna have a lot more arrays than 1 if it's grabbing the whole entire HTML page so I would have to use a "foreach" loop right? the 2nd piece of code shown above is to strip all non-numeric characters from the string so its just a number.
     
  2. mline

    mline Newbie

    Joined:
    Jan 30, 2010
    Messages:
    49
    Likes Received:
    18
    I would suggest a regular expression. I don't know enough about what you're doing to make one for you but I can show you, basicly, how it would work.

    Code:
    Dim matchCol As Regex.MatchCollection
    
    matchCol = Regex.Matches(FULLHTMLPAGE, "YOUR_REGULAR_EXPRESSION")
    
    For Each rMatch As Match in matchCol
       Debug.Writeline(rMatch.Value)
    Next
    I'd suggest getting RegexBuddy. It helps you to create your RegularExpressions and can generate language specific code.
     
    • Thanks Thanks x 1
    Last edited: Apr 3, 2010
  3. mline

    mline Newbie

    Joined:
    Jan 30, 2010
    Messages:
    49
    Likes Received:
    18
    Replacing non-numerics is also very easy with a regular expression. It would simply be this:

    Code:
    Dim newTextOnlyNumbers As String = Regex.Replace(oldText, "[^\d]", "")
    
    [] - just a chacter group denotation
    ^ - tells the expression "any character except..."
    \d - digits 0-9

    So, essentially, that is telling Regex to replace anything that is NOT a digit with "" (nothing/null)
     
    • Thanks Thanks x 1
  4. mrwho

    mrwho Junior Member

    Joined:
    Apr 3, 2008
    Messages:
    122
    Likes Received:
    39
    thanks mline for the help so far it appears the code you posted is for VBScript, I need this in C#(CSharp)...I now got the program grabbing the right variable and comparing it fine with regex but now I need to figure out how you do a greater than less than conditional statement, is it possible with Regex?
    heres the snippet of code in C#:
    Code:
    string[] source = html.Split(new char[] { ' ' });
                foreach (string cash in source)
                {
                    Regex exp = new Regex(@"(54,000,000)", RegexOptions.None);
                    Match m = exp.Match(cash);
                    while (m.Success)
                    {
                        MessageBox.Show("String has been found");
    
                    }
     
    Last edited: Apr 3, 2010
  5. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    To do a greater/less than, you'd have to convert the string to an int. Make sure all of the info in the string is a numerical value and then you can do:

    Code:
    if (int.Parse(stringName) > int.Parse(stringName2))
    {
       //it's what ever, what ever hoe!
    }