Results 1 to 14 of 14

Thread: help extract this string

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Exclamation help extract this string

    i am getting my data via winsock
    and this is what i get , i need to extract the blue coloured string to list1 please
    </td></tr><tr><td class="label">Reciprocal Link URL:</td><td class="field"><input type="text" name="RECPR_URL" value="" size="40" maxlength="255" class="text" /><br /><p class="small">To validate the reciprocal link please include the<br />following HTML code in the page at the URL<br />specified above, before submiting this form:</p><textarea name="RECPR_TEXT" rows="2" readonly="readonly" cols="37" class="text"><a href="http://www.xxx.org">;xx.Org Human Edited Web Directory</a>;</textarea></td></tr><tr><td class="label"><span class='req'>*</

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: help extract this string

    This is probably the easiest way of doing it:

    Code:
    List1.AddItem "<a href=""http://www.xxx.org"">;xx.Org Human Edited Web Directory</a>"
    If that does not work for you, then you need to be more specific about your requirements.



    BTW, are you sure your code isn't doing anything illegal?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: help extract this string

    bonnie i need to extract the string highlighted in blue , as u se all that data is in text1.text and by clicking command1 i want that string extracted from text1 to list1


    yes this is the data i need
    List1.AddItem "<a href=""http://www.xxx.org"">;xx.Org Human Edited Web Directory</a>"

    but this will only just add it not extract it from text1 , i wish to extract this data dear "<a href=""http://www.xxx.org"">;xx.Org Human Edited Web Directory</a>" from the string above to list1

  4. #4

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: help extract this string

    ok here it is working , reference for future use someone may need this piece of function is awsome

    Code:
    Public Function InStrBetween(ByRef Search As String, ByRef StringBegin As String, ByRef StringEnd As String, Optional ByVal Start As Long = 1, Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As String
                Dim lngLenSea As Long, lngLenBeg As Long, lngLenEnd As Long
                Dim lngBegin As Long, lngEnd As Long
                Dim strSearch As String, strBegin As String, strEnd As String
                ' get string lengths
                lngLenSea = LenB(Search)
                lngLenBeg = LenB(StringBegin)
                lngLenEnd = LenB(StringEnd)
                ' make sure we have lengths and a valid starting position
                If (lngLenSea <> 0) And (lngLenBeg <> 0) And (lngLenEnd <> 0) And (Start > 0) Then
                    ' make start a byte position
                    Start = ((Start - 1) * 2) + 1
                    ' case sensitive?
                    If Compare = vbBinaryCompare Then
                        ' find the starting position
                        lngBegin = InStrB(Start, Search, StringBegin, vbBinaryCompare)
                        ' because InStrB finds byte positions, we have to ensure we do not get "mid character" positions
                        Do While ((lngBegin And 1) = 0) And (lngBegin > 0)
                            lngBegin = InStrB(lngBegin + lngLenBeg, Search, StringBegin, vbBinaryCompare)
                        Loop
                        If lngBegin Then
                            lngBegin = lngBegin + lngLenBeg
                            ' find the ending position
                            lngEnd = InStrB(lngBegin, Search, StringEnd, vbBinaryCompare)
                            ' because InStrB finds byte positions, we have to ensure we do not get "mid character" positions
                            Do While ((lngEnd And 1) = 0) And (lngEnd >= lngBegin)
                                lngEnd = InStrB(lngEnd + lngLenEnd, Search, StringEnd, vbBinaryCompare)
                            Loop
                            ' make sure we have something
                            If lngEnd > lngBegin Then
                                ' return the result
                                InStrBetween = MidB$(Search, lngBegin, lngEnd - lngBegin)
                            End If
                        End If
                    Else
                        ' make upper case copies
                        strSearch = UCase$(Search)
                        strBegin = UCase$(StringBegin)
                        ' find the starting position
                        lngBegin = InStrB(Start, strSearch, strBegin, vbBinaryCompare)
                        ' because InStrB finds byte positions, we have to ensure we do not get "mid character" positions
                        Do While ((lngBegin And 1) = 0) And (lngBegin > 0)
                            lngBegin = InStrB(lngBegin + lngLenBeg, strSearch, strBegin, vbBinaryCompare)
                        Loop
                        If lngBegin Then
                            lngBegin = lngBegin + lngLenBeg
                            ' make upper case copy
                            strEnd = UCase$(StringEnd)
                            ' find the ending position
                            lngEnd = InStrB(lngBegin, strSearch, strEnd, vbBinaryCompare)
                            ' because InStrB finds byte positions, we have to ensure we do not get "mid character" positions
                            Do While ((lngEnd And 1) = 0) And (lngEnd >= lngBegin)
                                lngEnd = InStrB(lngEnd + lngLenEnd, strSearch, strEnd, vbBinaryCompare)
                            Loop
                            ' make sure we have something
                            If lngEnd > lngBegin Then
                                ' return the result
                                InStrBetween = MidB$(Search, lngBegin, lngEnd - lngBegin)
                            End If
                        End If
                    End If
                End If
            End Function

    Private Sub Command4_Click()
    Text2.Text = InStrBetween(Text1.Text, Text3.Text, Text4.Text, , False)
    End Sub

    text2.text ' results

    text1.text = </td></tr><tr><td class="label">Reciprocal Link URL:</td><td class="field"><input type="text" name="RECPR_URL" value="" size="40" maxlength="255" class="text" /><br /><p class="small">To validate the reciprocal link please include the<br />following HTML code in the page at the URL<br />specified above, before submiting this form:</p><textarea name="RECPR_TEXT" rows="2" readonly="readonly" cols="37" class="text"><a href="http://www.xxx.org">;xx.Org Human Edited Web Directory</a>;</textarea></td></tr><tr><td class="label"><span class='req'>*</



    text3.text has readonly="readonly" cols="37" class="text">

    text4.text has </textarea>


    no dear not illigal i am extracting data out of my site making a aio tool for seo thanks tho bonnie






    bonus , i like the top function betta

    Code:
    Private Function GetBetween(ByRef sSearch As String, _
         ByRef sStart As String, _
         ByRef sStop As String, _
         Optional ByVal lSearch As Long = 1, _
         Optional ByVal bCaseSensitive As Boolean = True) As String
         
         Dim lonS As Long, lonE As Long
         Dim strLCSearch As String
         Dim strLCStart As String, strLCStop As String
         
         'Case-sensitive...
         If bCaseSensitive Then
      lonS = InStr(lSearch, sSearch, sStart)
      If lonS Then
          lonS = lonS + Len(sStart)
          lonE = InStr(lonS, sSearch, sStop)
          If lonE Then
       GetBetween = Mid$(sSearch, lonS, lonE - lonS)
          End If
      End If
         
         'Not case-sensitive
         'Faster to convert string to lowercase rather than vbTextCompare
         Else
      strLCSearch = LCase$(sSearch)
      strLCStart = LCase$(sStart)
      strLCStop = LCase$(sStop)
      lonS = InStr(lSearch, strLCSearch, strLCStart)
      If lonS Then
          lonS = lonS + Len(strLCStart)
          lonE = InStr(lonS, strLCSearch, strLCStop)
          If lonE > 0 Then
       GetBetween = Mid$(sSearch, lonS, lonE - lonS)
          End If
      End If
         End If
     End Function

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: help extract this string

    Code:
    Private Sub Command1_Click()
        Dim StartPos As Long, EndPos As Long, sText As String
    
        sText = Text1
        StartPos = InStr(1&, sText, "<a href", vbTextCompare)
        If StartPos Then
            EndPos = InStr(StartPos + 7&, sText, "</a>", vbTextCompare)
            If EndPos Then
                List1.AddItem Mid$(sText, StartPos, (EndPos + 4&) - StartPos)
            End If
        End If
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: help extract this string

    @ladoo: Weren't you shown how to do this sort of thing here: http://www.vbforums.com/showthread.p...t=#post4431681 ?

  7. #7

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: help extract this string

    simple clean code , very affective very good thanks allot!!!!!!!!!!!!!! bonnie

    i was searching for it @doogle didnt find it this is why i posted

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: help extract this string

    Very good, Bonnie. Just curious about one thing. I never added the ampersand in my code after the 1, 7, and 4 that you show in your code example. Why is it there in yours?
    Doctor Ed

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: help extract this string

    @Doc: By default literal numbers in VB are of type Integer, suffixing a literal with a '&' forces it to be of type Long. Bonnie's code is avoiding an implied CLng when the code is executed.

  10. #10
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: help extract this string

    Quote Originally Posted by Doogle View Post
    @Doc: By default literal numbers in VB are of type Integer, suffixing a literal with a '&' forces it to be of type Long. Bonnie's code is avoiding an implied CLng when the code is executed.
    That's what I figured. In this thread question, I doubt that any number larger than integer would ever be encountered. Years ago we always used integer for speed whenever possible. Forcing it to long seems unnecessary here. Just MHO.
    Doctor Ed

  11. #11
    Lively Member
    Join Date
    May 2009
    Location
    UK
    Posts
    72

    Re: help extract this string

    try this :

    the text is in a textbox control : Text
    The search text gets sent to the listbox : List

    The function is executed by pressing a button control : Command

    Code:
    Private Sub Command_Click()
        Dim ThisText As String
        Dim SearchText As String
        
        SearchText = Text.Text
        ThisText = SearchID(SearchText, Chr$(34) & "text" & Chr$(34) & ">", ";</text", True)
        
        If ThisText <> "" Then Form1.List.AddItem ThisText
    
    End Sub
    
    Public Function SearchID(ScanString As String, StartMarker As String, FinishMarker As String, Optional CaseRequired As Boolean = False) As String
        Dim a As Integer
        Dim B As Integer
        Dim OriginalString As String
        
        SearchID = ""
        OriginalString = ScanString
        
        If CaseRequired = False Then
           ScanString = LCase$(ScanString)
           StartMarker = LCase$(StartMarker)
           FinishMarker = LCase$(FinishMarker)
        End If
        
        
        a = InStr(1, ScanString, StartMarker)
        If a <> 0 Then
            B = InStr(a + 1, ScanString, FinishMarker)
            If B <> 0 Then
    
                SearchID = Mid$(OriginalString, a + Len(StartMarker), B - a - Len(StartMarker))
    
            End If
    
        End If
    End Function
    Last edited by asymetrix; Jun 17th, 2013 at 11:22 PM.

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: help extract this string

    Quote Originally Posted by Code Doc View Post
    I doubt that any number larger than integer would ever be encountered.
    I don't know, some Web pages get very large in terms of number of characters, especially those with embedded Java and the like. The home page of these forums is about 8,500 words and at an average of 4 characters (including spaces) per word gives 34,000 characters, enough to explode an Integer.

    (Murphy's Law states that the items required will be within the last 50 characters or so on the page if Integers have been used and within the first 50 if Longs have been used)

    It's my understanding that using Longs rather than Integers is more efficient.

  13. #13
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: help extract this string

    However, if StartPos and EndPos are both defined as Long as she did in her code, then using an integer in between the two has got to be safe in my book. In this problem, you would never have a string length of over 32K characters between the two.

    I do not see how using longs rather than integers could ever result in more efficient code unless the upper bound is exceeded and stops the program dead in its tracks.
    Doctor Ed

  14. #14
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: help extract this string

    Quote Originally Posted by Code Doc View Post
    I do not see how using longs rather than integers could ever result in more efficient code unless the upper bound is exceeded and stops the program dead in its tracks.
    Hi Doc!

    I was just following this tip from the manual:

    Quote Originally Posted by MSDN
    Use Long Integer Variables and Integer Math

    For arithmetic operations avoid Currency, Single, and Double variables. Use Long integer variables whenever you can, particularly in loops. The Long integer is the 32-bit CPU's native data type, so operations on them are very fast; if you can’t use the Long variable, Integer or Byte data types are the next best choice.
    I usually tend to favor speed over memory. That's why I typically use the appropriate type for my variables, constants and numeric literals even if they consume more memory. As mentioned already by Doogle, I prefer to avoid run-time data type conversions whenever possible.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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