[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:
<input type="hidden" name="name1" value="value1">
<input type="hidden" name="name2" value="value2">
<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:
<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:
'// This is the pattern we want to match the hidden fields
Dim fieldSource As New Regex("(?<=""hidden"" name="").*?(?="")", _
RegexOptions.IgnoreCase Or RegexOptions.Singleline)
'// [Hidden Name Fields Count] This hold all the matches returned
Dim classMatches As MatchCollection = fieldSource.Matches(HTML)
'// Return the number of hidden fields
Dim numHiddenreFields As Integer = classMatches.Count
'// File location
Dim file As String = "Debug/xForm(4)-(file).xml"
'// The XmlTextWriter that will build the XML file
Dim myX As New Xml.XmlTextWriter(file, System.Text.Encoding.UTF8)
'// Add a few extra options to the XML
With myX
.Formatting = Formatting.Indented
.Indentation = 3
'// Write an element (this one is the root)
myX.WriteStartDocument()
myX.WriteStartElement("hiddenfileFields")
myX.WriteStartElement("hiddenfileChallenge")
myX.WriteStartElement("hiddenfileChallengeField")
myX.WriteValue(ChallengeField)
myX.WriteEndElement()
myX.WriteEndElement()
'// Use a for loop to go through tha array values
For Z As Integer = 0 To numHiddenreFields - 1
'1)
'// Set up the regular expression
Dim hiddenNames As New Regex("(?<=""hidden"" name="").*?(?="")", _
RegexOptions.IgnoreCase Or RegexOptions.Singleline)
'// This is an array of the files we don't want
Dim sourceNames As MatchCollection = hiddenNames.Matches(HTML)
' ''2)
' ''// Set up the regular expression
'Dim hiddenValues As New Regex("(?<=<input type=""hidden"" name=""" & sourceNames(Z).ToString & """ value="").*?(?="")", _
' RegexOptions.IgnoreCase Or RegexOptions.Singleline)
''// Put all the values in a collection
'Dim sourceValues As MatchCollection = hiddenValues.Matches(HTML)
'For Each sourceValues2 As Match In sourceValues
' MessageBox.Show(sourceValues2.ToString)
'Next
'// Write the values to the xml file
myX.WriteStartElement("reFields" & (Z))
myX.WriteValue(sourceNames(Z).ToString)
myX.WriteEndElement()
Next
'// Close up and end document
myX.WriteEndDocument()
myX.Close()
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
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