about Grab$
issueid=535 20-10-2017 10:20
Member
Number of reported issues by primo: 8
about Grab$
Grab$ confused in this case....

using the example posted by Eros days ago as a template
suppose we have this string (or a DNA sequence as an example)
sXML = "the apple on the first tree okay the apple on the second tree okay"
and we want to Grab$ what is between "apple" and "tree" globally
the Grab$ will return the results correctly
"on the first"
"on the second"


but if we add another "apple" like this:
sXML = "the apple on the apple first tree okay the apple on the second tree okay"
the Grab$ will be confused and output these results:
on the
first
on the second


#MinVersion 1.10.4
 
Uses "Console"

Global sXML As String
String    sCD
String    sTitle
Long      TxtIndex
  
  '---Print XML content
  'sXML = "the apple on the first tree okay the apple on the second tree okay"
  sXML = "the apple on the apple first tree okay the apple on the second tree okay"
  PrintL
  PrintL sXML In %CCOLOR_FYELLOW
   
  PrintL "---Now Parsing XML using GRAB$..." In %CCOLOR_FWHITE
     
  '---Loop till we have a <CD> node
  TxtIndex = 1
  
  Do
    '---Grab all <CD></CD> nodes
    sCD = Grab$(sXML, "apple", "tree", TxtIndex)
   
    If Len(sCD) Then
      '---From <CD> node get <TITLE> node
      sTitle = Grab$(sCD, "apple", "tree")
 
      PrintL TxtIndex, "CD Title:", sTitle  In %CCOLOR_FCYAN
      PrintL $TAB, "<CD> node content"  In %CCOLOR_FLIGHTMAGENTA
      PrintL sCD
       
      Incr TxtIndex
    End If
     
  Loop While Len(sCD)

 
PrintL "---All done, press a key to end---" In %CCOLOR_FYELLOW
 
 
WaitKey
a semi equivalent regex does not confused by adding another "apple" and will output correct results:
for the string sTXT = "the apple on the apple first tree okay the apple on the second tree okay"
and pattern "apple.*?tree"
Total matches found: 2
--------------------------------------------------
Total matches found: 2
--------------------------------------------------
Match number 1 found at position: 5 length: 29
Value is: apple on the apple first tree
--------------
Match number 2 found at position: 44 length: 24
Value is: apple on the second tree
--------------

--------------
the demo adapted from thinbasic example about regular expressions:
  '---The following code illustrates how to obtain a SubMatches collection from a regular 
  '---expression search and how to access its individual members.

  Uses "VBREGEXP", "Console"
  
  dim lpRegExp  as dword
  dim lpMatches as dword
  dim lpMatch   as dword
  Dim strValue, sTxT As String


  '---Allocate a new regular expression instance
  lpRegExp = VBREGEXP_New
  sTXT = "the apple on the apple first tree okay the apple on the second tree okay"
  '---Check if it was possible to allocate and if not stop the script
  if isfalse lpRegExp then
    MSGBOX 0, "Unable to create an instance of the RegExp object." & $crlf & "Script terminated"
    stop
  end if
  
  '---Set pattern
  VBRegExp_SetPattern lpRegExp, "apple.*?tree"
  '---Set case insensitivity
  VBREGEXP_SetIgnoreCase lpRegExp, -1
  '---Set global applicability
  VBRegExp_SetGlobal lpRegExp, -1

  '---Execute search
  lpMatches = VBRegExp_Execute(lpRegExp, sTxT)
  IF ISFALSE lpMatches THEN
    MSGBOX 0, "1. No match found"
  else
    
    dim nCount as long value VBMatchCollection_GetCount(lpMatches)
    IF nCount = 0 THEN
      MSGBOX 0, "2. No match found"
    else

      '---Iterate the Matches collection
      dim I as long

      strValue += "Total matches found:  " & nCount & $CRLF & string$(50, "-") & $crlf
      FOR i = 1 TO nCount

        lpMatch = VBMatchCollection_GetItem(lpMatches, i)

        IF ISFALSE lpMatch THEN EXIT FOR
        
        strValue += "Match number " & i & " found at position: " & VBMatch_GetFirstIndex(lpMatch) & " length: " & VBMatch_Getlength(lpMatch) & $CRLF
        strValue += "Value is: " & VBMatch_GetValue(lpMatch) & $CRLF
        strValue += "--------------" & $CRLF
        
        VBREGEXP_Release lpMatch
        
      NEXT
      
      'MSGBOX 0, strValue
      PrintL strValue 
     
    END IF
    
  END IF
  
  IF istrue lpMatches  THEN VBREGEXP_Release(lpMatches)
  IF istrue lpRegExp   THEN VBREGEXP_Release(lpRegExp)
  WaitKey()
Issue Details
Issue Number 535
Project thinBasic
Category Unknown
Status Unconfirmed
Priority Unknown
Affected Version 1.10.2
Fixed Version (none)
Milestone (none)
Users able to reproduce bug 0
Users unable to reproduce bug 0
Assigned Users (none)
Tags (none)




+ Reply