Results 1 to 9 of 9

Thread: String Parsing

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    99

    Arrow

    can anyone help me or tell me if this is possible.

    is there any way i can search a string lets say:

    "this is a string <url> www.vb-world.net </url> this is a string"

    and return the value between the <url> </url> tags?

    what if there were no space like this:
    "<url>www.vb-world.net<url>"

    any help woudl be greatly appreciated.

    thanks

  2. #2
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    Is a way, maybe not the best but....

    Code:
    Private Sub Command1_Click()
    MsgBox Trim(Replace(Replace("<url>www.vb-world.com</url>", "<url>", "", 1), "</url>", "", 1))
    End Sub

  3. #3
    Guest
    I used code similar to this to search for and report multiple instances of a certain item in over 10,000 html pages. And not every page even contained the string, and each was different, but always in between two constant characters (but not tags, so it made it a little more fun). Prior to this section of code, I have created an instance of a filesystemobject, that is simply looping through each file in the directory (strFileContents = objTextStream.ReadAll) Not the prettiest code in the world, but works like a charm! Good Luck!
    Code:
    Private Sub cmdGo_Click() 
      strBegin = "<url>"
      strEnd = "</url>
    
                Do While InStr(1, strFileContents, strBegin, 1) > 0
                    intPos = InStr(1, strFileContents, strBegin)
                    
                    ' next, find the point at which the string  can be extracted
                    intEndPos = InStr(intPos, strFileContents, strEnd)
                    
                    ' begin the oh so fun task of extracting
                    strURL = Mid(strFileContents, intPos, intEndPos - intPos)
                    strURL = Replace(strURL, strBegin, "")
                    
                    ' insert code here to do whatever you need it to do
                    ' with the current strURL, i simply placed the information
                    ' that i extracted into a db table
                    
                    strTemp = Right(strFileContents, Len(strFileContents) - intPos)
                    strFileContents = strTemp
                Loop
    End Sub

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Unhappy Problem...

    I played with your code idover. It works until intEndPos - intPos is > 0. then an invalid procedure occurs. You can't have a negative number in the mid function.

    Code:
    strURL = Mid(strFileContents, intPos, intEndPos - intPos)
    won't work if for example:

    strUrl = Mid(STRING,45,-65)

    -65 is no good....

    add this to the code:
    Code:
    Private Sub cmdGo_Click() 
      strBegin = "<url>"
      strEnd = "</url>"
          Do While InStr(1, strFileContents, strBegin, 1) > 0
             intPos = InStr(1, strFileContents, strBegin)
             intEndPos = InStr(intPos, strFileContents, strEnd)
    IF intPos > intEndPos then goto Skip 'ADD THIS LINE!
      strURL = Mid(strFileContents, intPos, intEndPos - intPos)
      strURL = Replace(strURL, strBegin, "")
                    
    ' insert code here to do whatever you need it to do
    ' with the current strURL, i simply placed the information
    ' that i extracted into a db table
        
    Skip:        'AND THIS LINE!    
    strTemp = Right(strFileContents, Len(strFileContents)-intPos)
    strFileContents = strTemp
    Loop
    End Sub
    this way if the intEndPos - intPos is a negative number..it skips it. (this most likely happens at the end of the search). Now I am no expert...but this seem to work for me. There is most likely a better way (there always is isn't ther!) :P

    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Guest
    This code worked for me just fine. But you are right, you can't use a negative number in the Mid function. However, there are no negative positions in a text file. The code as shown works from the beginning of the current string, (which is shortened with each occurance of your search string) and searches for and records the position of the tags "<url>, </url>". The code is designed to never begin a search prior to the position of the previous occurance, hence the shortening of the entire string. It seems to me that the only way that you could get a a negative number is if the string is not being shortened for some reason and a closing tag is found before its companion opening tag, signifying a missing tag. You should try counting the number of opening and closing tags, and make sure that they're equal.

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Wink true...

    Thats true...
    What I used to test it was...created a textbox.
    dumped this into it:

    can anyone help me or tell me if this is possible.

    is there any way i can search a string lets say:

    "this is a string <url> http://www.vb-world.net </url> this is a string"

    and return the value between the <url> </url> tags?

    what if there were no space like this:
    "<url>www.vb-world.net<url>"

    any help woudl be greatly appreciated.

    thanks
    and it came up with the error...

    I just noticed....this:
    "<url>www.vb-world.net<url>"
    2 openening tags.....

    Bingo!


    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Guest
    Great!... Hopefully this fixed your problem?...

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    99

    Smile thanks

    Thanks so much, i actually though of an algorithm before i went to sleep that looks like this. It works quite well too.

    _________________________________________________________
    Public Function stringBetween(word As String, startstring As String, endstring As String) As String

    Dim first As Integer
    Dim last As Integer

    first = InStr(word, startstring) + Len(startstring) 'finds pos at end of first word
    last = InStr(word, endstring) ' finds the pos at the beginning of the second word

    If (last <= first) Then 'prevents errors
    stringBetween = "Not Found"
    Else
    stringBetween = Mid(word, first, (last - first)) 'returns the string in between
    End If

    End Function

  9. #9
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    another

    the function::::

    Private function HighlightFonts(txt)
    Dim rst

    if not txt = "" then
    txt = replace(txt,"whatever"," what ever")
    end if
    HighlightFonts=txt

    end function



    the call
    <%=HighlightFonts(rst.fields("SUBHEADING"))%>

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