|
-
Sep 6th, 2009, 08:28 AM
#1
Thread Starter
Hyperactive Member
[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
-
Sep 6th, 2009, 04:46 PM
#2
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
-
Sep 6th, 2009, 04:56 PM
#3
Addicted Member
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
-
Sep 6th, 2009, 05:32 PM
#4
Thread Starter
Hyperactive Member
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:
<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
-
Sep 7th, 2009, 02:12 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|