regular expressions in vb6

Discussion in 'Code Snippets and Tutorials' started by ceneret0023, Aug 3, 2009.

  1. ceneret0023

    ceneret0023 Level III

    Joined:
    Jun 10, 2007
    Messages:
    663
    Likes Received:
    15
    Location:
    under a rock
    I'm currently working on an igloo aber and i've run into a problem with finding the item id. I can't grab the specific item id with a getstring function so i figured i'd try a regular expression even though i didn't fully understand them. I'll have an expression that roughly corresponds to this
    Code (Text):
    1. regexp.pattern = "process_igloo/.phtml_obj_id=(/d*) onclick/( /!/( 'Are you sure you wish to buy" & ShopList(i)
    i know this probably isn't the correct info in the means of what is actually in the source because i'm too lazy to open up my code and track it down . The only problem is when i try to test this on strHTML
    Code (Text):
    1. regexp.test(strHTML)
    it gives me an error saying the function test could not be performed on the obj regexp. I've also tried simplifying it to just getting
    Code (Text):
    1. regexp.pattern = "process_igloo/.phtml_obj_id=(/d*)"
    to see if there was a problem past getting the id but it continues to give me an error. Again i've simplified it even more to just using a login the would grab the welcom, username NP: np and it doesn't give me an error but when i try to test my regular expression it can't find it
    Code (Text):
    1. regexp.pattern = "Welcome, (.*) | NP: (/d*)"
    and the test will always return as a failed test even though i'm positive my login function works and if i try and execute it, it returns a null value. So can somebody please help explain to me what i'm doing wrong? i've googled it and all my findings just don't seem to help me figure out whats wrong.

    also here's my login function that grabs the welcome, username | NP: np

    Code (Text):
    1. Private Sub login(Username As String, Password As String)
    2.     strHTML = Wrapper.GetWrapper("http://www.neopets.com/loginpage.phtml", "http://www.neopets.com/index.phtml")
    3.     lblStatus.Caption = "Logging in (1/3)"
    4.     strHTML = Wrapper.PostWrapper("http://www.neopets.com/hi.phtml", "destination=%2Findex.phtml&username=" & Username, "http://www.neopets.com/loginpage.phtml")
    5.     lblStatus.Caption = "Logging in (2/3)"
    6.     strHTML = Wrapper.PostWrapper("http://www.neopets.com/login.phtml", "destination=%2Findex.phtml&username=" & Username & "&password=" & Password, "http://www.neopets.com/hi.phtml")
    7.     If InStrB(1, strHTML, "Location: /index.phtml") Then
    8.         lblStatus.Caption = "Logged In"
    9.         Set RegEx = New RegExp
    10.         RegEx.Pattern = "Welcome, (.*) | NP: (/d)"
    11.         If RegEx.Test(strHTML) = True Then
    12.             Set colMatches = RegEx.Execute(strHTML)
    13.             For Each Match In colMatches
    14.                 MsgBox (Match.Value)
    15.             Next
    16.         Else
    17.             MsgBox ("could not match string")
    18.         End If
    19.     ElseIf InStrB(1, strHTML, "FROZEN") Then
    20.             lblStatus.Caption = "Account Frozen"
    21.             cmdLogin.Enabled = True
    22.     ElseIf InStrB(1, strHTML, "Location: /badpassword.phtml") Then
    23.             lblStatus.Caption = "Bad Password"
    24.             cmdLogin.Enabled = True
    25.          End If
    26. End Sub
    27.  
    and i'd also be happy to pm trusted members the code i have for my igloo process although i'd have to clean it up so other people besides me can understand what's going on xD
     
  2. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Here's what I use in my iglooAber

    The getbetween function
    Code (Text):
    1.     Public Function GetBetween(ByVal Source As String, ByVal Start As String, ByVal Finish As String) As String
    2.         Dim Result = ""
    3.         Dim A = InStr(1, Source, Start) + Len(Start)
    4.         If A = 0 Then
    5.             Result = ""
    6.             Return Result
    7.         Else
    8.             Dim B = InStr(A, Source, Finish)
    9.             If B = 0 Then
    10.                 Result = ""
    11.                 Return Result
    12.             Else
    13.                 Result = Mid(Source, A, B - A)
    14.                 Return Result
    15.             End If
    16.         End If
    17.     End Function
    And then, I check if there's a item in my list (lstList) can be found at the igloo and grab the ID to create the final "GET" to buy the item. FYI, "Response" is the return of the wrapper, so in this case, the igloo page. I remove all the text after the item and use the InStrRev to do the search.

    Code (Text):
    1. If Response.Contains(lstList.Items(i).ToString + " at") Then
    2. Response = Response.Remove(InStr(Response, lstList.Items(i).ToString) + lstList.Items(i).ToString.Length + 20, Response.Length - (InStr(Response, lstList.Items(i).ToString)) - lstList.Items(i).ToString.Length - 21)
    3.                             Response = Response.Remove(0, InStrRev(Response, "process_igloo"))
    4. Dim ItemLink As String = "http://www.neopets.com/winter/process_igloo.phtml?obj_info_id=" + Wrapper.GetBetween(Response, "obj_info_id=", "'")
    5.                             Response = Wrapper.Request("GET", ItemLink, "http://www.neopets.com/winter/igloo2.phtml")
    6. End If
    It's a bit messy but it work.
    There's some validation to do after if you bought an item but that's about it.


    Edit: Whitch wrapper are you using ??? The login seems fine.
     
  3. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Hmm, the escape character is "\" and not "/"... also digits are "\d" and not "/d" xD You inverted the two symbols... However, could you please post a snippet of the HTML you're trying to match? This way I can test it myself and check if the regex works.
     
    ceneret0023 likes this.
  4. ceneret0023

    ceneret0023 Level III

    Joined:
    Jun 10, 2007
    Messages:
    663
    Likes Received:
    15
    Location:
    under a rock
    oh wow i feel retarded xD i never would have caught that and that was the problem xD it all works now lol i can't believe i was so dumb and didnt realize it xD +rep ricky and tharoux thanks for the getstring function i'll have to try and convert it to vb6 some time so i can have a better getstring function :)

    oh and also one last thing no matter how many times i read things about how to use back references i can never understand. Is it possible to use a backreference outside of the regular expression? like for example this is the regular expression i have
    Code (Text):
    1. RegEx.Pattern = "process_igloo\.phtml\?obj_info_id=(\d*)' onClick=" & ChrW(34) & "if \( \!confirm\('Are you sure you wish to buy " & ShopList(i) & " at (\d*) NP\?'\)"
    to get to the item id i know you have to do something like ()\n or $1 but i could never figure it out...
     
  5. sLAUGHTER

    sLAUGHTER Newbie

    Joined:
    Apr 2, 2008
    Messages:
    4
    Likes Received:
    8
    Location:
    Jersey
    to return things like that ceneret, you need to do this...

    Code (Text):
    1. RegEx.Pattern = "process_igloo\.phtml\?obj_info_id=(\d*)' onClick=" & ChrW(34) & "if \( \!confirm\('Are you sure you wish to buy " & ShopList(i) & " at (\d*) NP\?'\)"
    2. ObjID = RegEx.Replace(strHTML, "$1")
    3. Cost = RegEx.Replace(strHTML, "$2")
    Pretty sure that'll work...
     
  6. ceneret0023

    ceneret0023 Level III

    Joined:
    Jun 10, 2007
    Messages:
    663
    Likes Received:
    15
    Location:
    under a rock
    ok thanks for the help sLaughter but when i do put my code as that i get something like this
    [​IMG]

    the first line is the item name, the second line is supposed to be the ID, and the third is supposed to be the Cost, but for some reason when i try to get the backreference it gives me what looks to be server data o_O


    sorry if i'm sounding stupid or needy but no matter how much i read about regular expressions and back references it just doesn't click for me xD

    EDIT: nevermind i figured it out, i have to access the backreference from the match itself not strHTML which actually makes a lot of sense since the match would have the back reference stored xD meaning it'd look something like this
    Code (Text):
    1. regex.replace(match, "$1")