Results 1 to 5 of 5

Thread: [RESOLVED] Regexp for 2 values

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Resolved [RESOLVED] Regexp for 2 values

    Hi Guys,

    For the past 2 days i have been trying to come up with a simple solution for this problem, but i have turned up nothing so far.

    basically i have a few fields in some html named:

    vb.net Code:
    1. <input type="hidden" name="name1" value="value1">
    2. <input type="hidden" name="name2" value="value2">
    3. <input type="hidden" name="name3" value="value3">

    i can grab the name fields fine and also store them in an .xml file, but when i try to get the value fields and place them like:

    vb.net Code:
    1. <field>name1}{value1</field>

    I'm getting errors, basically because the name fields have 13 and the value fields have 14, so the numbers don't match up.

    my code below is a bit clumsy, i have deleted/undeleted so much:

    vb.net Code:
    1. '// This is the pattern we want to match the hidden fields
    2.         Dim fieldSource As New Regex("(?<=""hidden"" name="").*?(?="")", _
    3.                      RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    4.  
    5.         '// [Hidden Name Fields Count] This hold all the matches returned
    6.         Dim classMatches As MatchCollection = fieldSource.Matches(HTML)
    7.  
    8.         '// Return the number of hidden fields
    9.         Dim numHiddenreFields As Integer = classMatches.Count
    10.  
    11.         '// File location
    12.         Dim file As String = "Debug/xForm(4)-(file).xml"
    13.  
    14.         '// The XmlTextWriter that will build the XML file
    15.         Dim myX As New Xml.XmlTextWriter(file, System.Text.Encoding.UTF8)
    16.  
    17.         '// Add a few extra options to the XML
    18.         With myX
    19.             .Formatting = Formatting.Indented
    20.             .Indentation = 3
    21.  
    22.             '// Write an element (this one is the root)
    23.             myX.WriteStartDocument()
    24.             myX.WriteStartElement("hiddenfileFields")
    25.  
    26.             myX.WriteStartElement("hiddenfileChallenge")
    27.             myX.WriteStartElement("hiddenfileChallengeField")
    28.             myX.WriteValue(ChallengeField)
    29.             myX.WriteEndElement()
    30.             myX.WriteEndElement()
    31.  
    32.             '// Use a for loop to go through tha array values
    33.             For Z As Integer = 0 To numHiddenreFields - 1
    34.  
    35.                 '1)
    36.                 '// Set up the regular expression
    37.                 Dim hiddenNames As New Regex("(?<=""hidden"" name="").*?(?="")", _
    38.                                      RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    39.  
    40.                 '// This is an array of the files we don't want  
    41.                 Dim sourceNames As MatchCollection = hiddenNames.Matches(HTML)
    42.  
    43.  
    44.                 ' ''2)
    45.                 ' ''// Set up the regular expression
    46.                 'Dim hiddenValues As New Regex("(?<=<input type=""hidden"" name=""" & sourceNames(Z).ToString & """ value="").*?(?="")", _
    47.                 '                     RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    48.  
    49.                 ''// Put all the values in a collection
    50.                 'Dim sourceValues As MatchCollection = hiddenValues.Matches(HTML)
    51.  
    52.                 'For Each sourceValues2 As Match In sourceValues
    53.                 '    MessageBox.Show(sourceValues2.ToString)
    54.                 'Next
    55.  
    56.                 '// Write the values to the xml file
    57.                 myX.WriteStartElement("reFields" & (Z))
    58.                 myX.WriteValue(sourceNames(Z).ToString)
    59.                 myX.WriteEndElement()
    60.  
    61.             Next
    62.  
    63.             '// Close up and end document
    64.             myX.WriteEndDocument()
    65.             myX.Close()
    66.  
    67.         End With

    What i am thinking now, is if there was a way to get the "name" and "value" field with the 1 regular expression, instead of having to do the 2, would that be possible? or is there an easier way i could have done this?

    my brain aches from trying to work this out lol

    thanks for any help guys

    Graham

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Regexp for 2 values

    Hey,

    Are you familiar with using Groups within your Regular Expressions?

    You should be able to create groups, one for the name and one for the value, and reference those within your matches.

    You can find some more information on that here:

    http://msdn.microsoft.com/en-us/libr...ollection.aspx

    I hope that helps!!

    Gary

  3. #3
    Addicted Member
    Join Date
    Mar 2008
    Posts
    129

    Re: Regexp for 2 values

    Well it may not be pretty, but this is what I came up with on a whim.. i tried the WAY SHORTER version of strbetween floating around, but came up with errors for some reason, so I made my own LONGER version that seems to GRAB what you need it too..

    Code:
    Function getinfo(ByVal Filter, ByVal SourceText)
            Dim info = Nothing
    
            Dim rx As New System.Text.RegularExpressions.Regex(Filter)
    
            Dim m As Match
    
            For Each m In rx.Matches(SourceText)
    
                info = info & _
                "<field>" & _
                strbetween(m.ToString, "", "name=" & Chr(34), Chr(34)) & _
                "}{" & _
                strbetween(m.ToString, "", "value=" & Chr(34), Chr(34)) & _
                "</field>" & vbCrLf
    
            Next
            getinfo = info
    
        End Function
        Public Function strbetween(ByVal SearchText As String, ByVal findfirst As String, ByVal StartText As String, ByVal EndText As String)
            On Error GoTo 3
            Dim s
            Dim a
            Dim b
            Dim c
            Dim d
            s = InStr(1, SearchText, findfirst)
            If s = 0 Then
                strbetween = findfirst & " Not Found"
            End If
    
            a = InStr(s, SearchText, StartText)
            If a = 0 Then
                strbetween = StartText & " Not Found"
                GoTo 4
            Else
                a = a + Len(StartText)
            End If
            b = InStr(a, SearchText, EndText)
            If b = 0 Then
                strbetween = EndText & " Not Found"
                GoTo 4
            End If
            c = b - a
            d = Mid(SearchText, a, c)
            strbetween = d
            GoTo 4
    3:      strbetween = "An error occured"
    4:
        End Function
    Try this usage first to see if it grabs the information like you need it...

    Code:
    TextBox2.Text = getinfo("<input type=.*name=.*", TextBox1.Text)
    Textbox1.text = your html page
    textbox2.text = NOTHING until you use the above code..

    if that grabs it like you like it see if you can save that info to the xml

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Re: Regexp for 2 values

    Hi Guys, Thanks a ton for the help! i have been at this for a few days lol, what i came up with was this:

    Grab the whole string which looks like:

    vb Code:
    1. <input type="hidden" name="name1" value="value1">

    Grab this part using regex:

    <input type="hidden" name="name1" value="value1">

    Then replace: " value=" this bit with a + sign, split the values into (0) and (1) then use them.

    Thanks BlayzeX, that looks and works perfect, and gave me a few programming tips thanks mate

    looking into groups aswell Gary, thanks to mate.

    cheers

    Graham

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Regexp for 2 values

    Hey,

    I just had a quick look at the code that was posted by BlayzeX and I have a concern.

    I am by no means an expert in VB, but as far as I was aware, On Error GoTo, is certainly not a recommended approach anymore, and should be avoided.

    Much better exception handling can be done using Try/Catch.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width