We help IT Professionals succeed at work.

Parsing, Triming String. Need Help 500 Points.

bogdem
bogdem asked
on
193 Views
Last Modified: 2010-04-23
Hello, Experts!
Look, I have strings like this
<input id="cmdSearch" class="button" style="width: 78px" type="submit" value="Search" /><input id="cmdUp" class="button" style="width: 78px" type="submit" value="Up" />
<input id="cmdUp" class="button" style="width: 78px" type="submit" value="Up" /><a href="#"><img alt="" src="images/z1.gif"><img alt="" src="images/z1.gif">
<img alt="" src="images/z1.gif"><input id="cmdDown" class="button" style="width: 78px" type="submit" value="Down" /><img alt="" src="images/z1.gif"><img alt="" src="images/z1.gif">

I need a function, so I can pass a String and get ID and value back
like:
cmdSearch=Search
cmdUp=Up

Please, help
Comment
Watch Question

Top Expert 2007

Commented:
Here's a function for you:

    Private Function GetID(ByVal str As String) As String
        Dim sReturn As String = String.Empty

        Dim iStart As Integer = str.IndexOf("id=")
        Dim iEnd As Int16 = str.IndexOf(Chr(32), iStart)

        sReturn = str.Substring(iStart + 3, iEnd - (iStart + 3))


        Return sReturn

    End Function



    '  Call it like this:
    dim str as string = "<input id='cmdSearch' class='button' style='width: 78px' type='submit' value='Search' />"

        MsgBox(GetID(str))

Author

Commented:
Look at my first string
<input id="cmdSearch" class="button" style="width: 78px" type="submit" value="Search" /><input id="cmdUp" class="button" style="width: 78px" type="submit" value="Up" />

is this string I have two IDs. Also it can be 3 or more IDs.
Top Expert 2007

Commented:
Oops, forgot the value.  Here you go:


        Dim returnString As String() = _
        GetID("<input id='cmdSearch' class='button' style='width: 78px' type='submit' value='Search' />")

        MsgBox("ID:  " & returnString(0) & vbCrLf & "Value:  " & returnString(1))



    Private Function GetID(ByVal str As String) As String()
        Dim sReturn(1) As String

        Dim iStart As Integer = str.IndexOf("id=")
        Dim iEnd As Integer = str.IndexOf(Chr(32), iStart)

        'Return ID
        sReturn(0) = str.Substring(iStart + 3, iEnd - (iStart + 3))

        iStart = str.IndexOf("value=")
        iEnd = str.IndexOf(Chr(32), iStart)

        'Return VALUE
        sReturn(1) = str.Substring(iStart + 6, iEnd - (iStart + 6))

        Return sReturn

    End Function
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2007

Commented:
Very fancy Fernando Soto!  I need to get a little better at Regex!

Here's an update to my humble code:

       'This is the code that can go in the sub where you original string is.
        Dim sHTML As String = _
            "<input id='cmdSearch' class='button' style='width: 78px' type='submit' value='Search' />" & _
            "<input id='cmdUp' class='button' style='width: 78px' type='submit' value='Up' />"

        Dim iStart As Integer = 0
        Dim iEnd As Integer = 0
        Do
            iStart = sHTML.IndexOf("<", iStart)

            If iStart > -1 Then
                iEnd = sHTML.IndexOf("/>", iStart)

                Dim returnString As String() = GetID(sHTML.Substring(iStart, iEnd - iStart))

                MsgBox("ID:  " & returnString(0) & vbCrLf & "Value:  " & returnString(1))

                iStart = iEnd + 1

            End If

        Loop Until iStart = -1



    'This is the function that does the parsing
    Private Function GetID(ByVal str As String) As String()
        Dim sReturn(1) As String

        Dim iStart As Integer = str.IndexOf("id=")
        Dim iEnd As Integer = str.IndexOf(Chr(32), iStart)

        'Return ID
        sReturn(0) = str.Substring(iStart + 3, iEnd - (iStart + 3))

        iStart = str.IndexOf("value=")
        iEnd = str.IndexOf(Chr(32), iStart)

        'Return VALUE
        sReturn(1) = str.Substring(iStart + 6, iEnd - (iStart + 6))

        Return sReturn

    End Function

Author

Commented:
Sorry VBRocks your code fail on String like
        Dim sHTML As String = _
            "<input id='cmdSearch' class='button' style='width: 78px' type='submit' value='Search' />" & _
            "<input id='cmdUp' class='button' style='width: 78px' type='submit' value='Up' /><input id='cmdDown' class='button' style='width: 78px' type='submit' value='Down' /><img alt='' src='images/z1.gif'><img alt='' src='images/z1.gif'>"

so, don't be mad all points goes to FernandoSoto, his code works with any string.
Thanks a lot.
CERTIFIED EXPERT

Commented:
bogdem, you will want to be careful to make sure that inside your <input> tags that id= is shown before value= and that their values are enclosed in quotes.  If the ordering inside the input tags vary in format then the regex pattern will need to be modified to allow for that.  The possible variations in source code complicates regex matching of HTML.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Not a problem bogdem, glad I was able to help. ;=)

@ VBRocks - Sometimes regex is not the best way to do things and sometimes it is. But it does cut down on the code you have to write to match string patterns. ;=)
Top Expert 2007

Commented:
No prob.  He did a good job!  maybe next time.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.