'---Script modified to work on Thinbasic 08-27-2023 03:10:48 by Gary Robinson
'TOPIC   REGEXPR and REGREPL demo  (023269)
'NAME    Michael Mattias
'DATE    2002-01-16 07:09


Uses "Console" 
uses "File"








   Dim Stat As Long
   Dim J$
   
   Print "Hello World from Program " 
   Console_WaitKey
   Reg1()
   Console_WaitKey




function Reg1()


   Dim TheFile As String, FileData As String ' , H AS LONG
   Dim I As Long
   Dim J$
   ' REGEXPR, REREPL variables
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String


'  ****    REGEXPR demos   *****
   'PbHelpFile()                        ' Tested  OK
   'PhoneNumbers()                      ' tested OK
   'AlternateSpelling()                 ' tested OK
   'FindEachWordInString()              ' tested OK
    'FindAllComments_InSourceCode()      'tested OK
   'ThreewayAlternation()               ' tested OK
   'RegExprLineInput()                  ' tested OK




' 
   ' CompressSpaces()                    ' tested OK
    'ReplaceEachWordInStringWithPlural()  ' tested OK
    'ParseCSVFile()                      ' tested OK
    CreateDeclareFile()                 ' tested OK


end function


function PBHelpFile()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   TheMain = "Send your comments to BASICally Speaking at bsp@infoms.com"


   'Mask    = "[a-z_.-]+@[a-z_.-]+"
   Mask    = "[html]+@[html]+"
   dim nStart as long,s as string
   nStart=1
   Print mask
   'mask =chgRe(mask)
   'Print mask
   REGEXPR$(Mask, TheMain, startvar, PosVar, LenVar)
   'RegExpr Mask In TheMain To PosVar, LenVar
   Print "REGEXPR returns:" & Mid$(TheMain, PosVar, lenVar)
   Console_WaitKey
End Function  


function AlternateSpelling()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   
  Mask = "Colo[u]?r"      ' a 'u' in the indicated position is optional
  TheMain = " The color of the leaves is more brilliant than the colour of the television."


' INPUT: TheMain, Mask.  OUTPUT: multiple REGEXPR results
  PosVar = 1:   StartVar = 1
  While PosVar
    REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar) 
    'RegExpr Mask In TheMain At StartVar To PosVar,Lenvar
    If PosVar Then
         TheWord = Mid$(TheMain, Posvar, LenVar)
         Print TheWord
         StartVar = PosVar + LenVar
    End If
  Wend
end function


Function PhoneNumbers()
 Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
 Mask = "\([3 digits]\)-[3 digits]-[4 digits]"
 Print mask
 'mask=chgRe(mask)
 'Mask = "\([0-9][0-9][0-9]\)-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"
 TheMain = " call me at (900)-123-4567"
 REGEXPR$(Mask, TheMain, PosVar, LenVar)
 'RegExpr Mask In TheMain To PosVar,LenVar
 If PosVar Then Print Mid$(TheMain, PosVar, LenVar)
 Console_WaitKey
end function


function FindEachWordInString()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
 
   TheMain = " Now is the time for    all good men" & $CrLf & "to come to the aid of his party."
   Let mask = "[1+ letters][wordboundary]"        ' \b = word boundary. CRLF is (undocumented) word boundary
   Print mask
   'mask=chgRe(mask)
   'GoSub RegExprLoop
   PosVar = 1:   StartVar = 1
  While PosVar
    REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar) 
    'RegExpr Mask In TheMain At StartVar To PosVar,Lenvar
    If PosVar Then
         TheWord = Mid$(TheMain, Posvar, LenVar)
         Print TheWord
         StartVar = PosVar + LenVar
    End If
  Wend
end function


function ThreewayAlternation()
  Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String


  TheMain = " Two is too many to do the job "
  Mask = "(to[wordboundary])|(too[wordboundary])|(two[wordboundary])"     ' works fine 2, 9, 18 found
  Print mask
  'mask=chgRe(mask)


  Mask = "(to)|(too)|(two)\b"         ' did not find too(9), but did find to (18) and Two (2)  BUGBUGBUG
'  Mask = "((to)|(too)|(two))\b"        ' found Two (2) and to (18) but not too (9)
'  Mask = "[(to)|(too)|(two)]\b"       ' found single 'o' chararters at 4, 11, 19 and 22
'  Mask = "[(to)|(too)|(two)]+\b"      ' found Two(2), too(9), to (18) and 'o' at 22 (in 'do')
 '  GoSub RegExprLoop
 PosVar = 1:   StartVar = 1
  While PosVar
    REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar) 
    'RegExpr Mask In TheMain At StartVar To PosVar,Lenvar
    If PosVar Then
         TheWord = Mid$(TheMain, Posvar, LenVar)
         Print TheWord
         StartVar = PosVar + LenVar
    End If
  Wend
end function


function RegExprLineInput()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   Dim TheFile as string,FileData as string
   TheFile = "Regexpression.BAS"
   FileData = LoadFile (TheFile)
   'Print "Size of file is " & Str$(Len(FileData))
   Console_WaitKey
   StartVar = 1: PosVar   = 1
   Mask = "\n"                       ' \n = newline
   While PosVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      'RegExpr Mask In FileData At StartVar To PosVar, LenVar
      If PosVar Then
         TheWord = Mid$(FileData, StartVar, PosVar - StartVar-1)  ' NOTE: must remove the newline character
         Print "x";TheWord;"x"
         StartVar = PosVar + LenVar
      End If
   Wend
end function


function CompressSpaces()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   dim retFromReg as string
   
   TheMain = " The color   of the    leaves is more brilliant than the colour of     the television."
    Mask    = "\x20[\x20]+"         ' find space followed by one or more spaces
   'mask =chgRe(mask)
   Print mask + $crlf
    ReplaceMask = "\x20"          ' replace with a single space.
   PosVar = 1 :  StartVar = 1
   Print "TheMain Begins as " & $Dq & TheMain & $Dq + $crlf
   While StartVar
      REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar)
      retFromReg=Mid$(TheMain,PosVar,LenVar)
       
      'replaces multiple spaces with one space
      newMain = REPLACE$(TheMain, retFromReg, " ")


      'RegRepl Mask In TheMain With ReplaceMask At StartVar To Posvar, NewMain
      StartVar = PosVar
      TheMain = NewMain
   Wend
   Print "TheMain Converted to "  & $Dq & NewMain & $Dq + $crlf
end function


function ReplaceEachWordInStringWithplural()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim Main As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain2 As String
   Dim ReplaceMask As String
   dim retFromReg as string
   dim newRetFromReg as string


   Main = "Now is the time for all good men" & $CrLf 
   'Find each word in a string      WORKS  9/17/99 even with embedded spaces, CRLF is an undocumented word boundary
    mask = "([A-Za-z]+)[\x20]"        ' not working
   Print mask + $crlf
   
   Main = "Now is the time for all good men" & $CrLf 
   print main
   ReplaceMask = "\01s"       ' for each word, append an 's'
   StartVar = 1: PosVar =1
   While StartVar
      REGEXPR$(Mask, Main, StartVar, PosVar, LenVar)
      retFromReg=Mid$(Main,PosVar,LenVar)
      
      newRetFromReg=trim$(retFromReg) + "s " 
      
      StartVar = PosVar+lenVar
      
      Main = REPLACE$(Main, retFromReg, newRetFromReg)
   Wend
   
   Print Main  + $crlf
end function


function ParseCSVFile()
  Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   dim FileData as string 


  ' file in:  "Value","value", "value",crlf, "value", "value", "value", Crlf.....
  FileData = $Dq & "Smith" & $Dq & "," & $Dq & "John"   & $Dq & "," & $Dq & "555-1234" & $Dq & $CrLf & _
             $Dq & "Doe"   & $Dq & "," & $Dq & "Jane"   & $Dq & "," & $Dq & "555-6789" & $Dq & $CrLf & _
             $Dq & "Brown" & $Dq & "," & $Dq & "Willie" & $Dq & "," & $Dq & "555-8765" & $Dq & $CrLf


  Dim WordMask As String


  Mask = "\q(.*)\q"
  Print mask + $crlf




  Print FileData + $crlf
  Print "*** END OF FILEDATA OF LEN=" & Str$(Len(FileData)) & "***" +$crlf


  Console_WaitKey
  dim retFromReg as string
  dim newRetFromReg as string
  PosVar = 1: StartVar = 1
  While StartVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      retFromReg=Mid$(Main,PosVar,LenVar)
      
      newRetFromReg=trim$(retFromReg)  
      
      StartVar = PosVar+lenVar
      
      Main = REPLACE$(Main, retFromReg, newRetFromReg)
   Wend
   
   Print Main  + $crlf  


  Print Filedata                        ' show the modified file
end function




function CreateDeclareFile()
  dim TheFile as string
  dim FileData as string
  
  Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String




  dim retFromReg as string
  dim newRetFromReg as string
  dim declaresFound as string
  TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
  FileData = File2Text(TheFile) 
  Mask = "^[\x20]*((SUB)|(FUNCTION))[\x20]+[a-z]+"   
   Print mask + $crlf
 
      ' first line of SUB/FUNCTIONs which use _ continuation to next line
  PosVar = 1
  Print "START OUTPUT for Mask = " & mask + $crlf
  
  PosVar = 1: StartVar = 1
  While StartVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      retFromReg=Mid$(FileData,PosVar,LenVar)
      declaresFound=declaresFound + "Declare "+retFromReg + $crlf
      
      StartVar = PosVar+lenVar
  Wend
'---------------------------------------------------------------------------------------
   Print declaresFound  + $crlf   








end function 


function FindAllComments_InSourceCode()
   dim TheFile as string
  dim FileData as string
  dim mask as string
  dim startvar as long
  dim posVar as long
  dim lenvar as long
  dim retFromReg as string
  TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
  FileData = File2Text(TheFile) 
  dim comments as string
  Mask = "'[\x20]*[A-Za-z0-9 ]*"
  
  
  PosVar = 1: StartVar = 1
  While StartVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      retFromReg=Mid$(FileData,PosVar,LenVar)
      comments=comments + retFromReg + $crlf
      
      StartVar = PosVar+lenVar
  Wend
   
   Print comments  + $crlf   


end function




Function File2Text(sfn As String) As String


    Dim st  As String
    Dim n   As Long
     Dim FileHandle As DWORD,s as string
    FileHandle= FILE_OPEN(sfn, "INPUT")  
    while not FILE_EOF(FileHandle)
       s=FILE_LineInput(FileHandle)
       st=st+s+$crlf 
    wend
    
    s = FILE_Close(FileHandle)
    Function=st
End Function


  Function   bbb()  As Long
     ' dummy function, used in FindAllProceduresInSourceCode; indented on purpose
  End Function


Function ManyParameters (A As Single, B As Double, C As long, Z_A As String ) As Long
   ' dummy function for demo
End Function




'------------------
'---Script modified to work on Thinbasic 08-27-2023 03:10:48 by Gary Robinson
'TOPIC   REGEXPR and REGREPL demo  (023269)
'NAME    Michael Mattias
'DATE    2002-01-16 07:09


Uses "Console" 
uses "File"








   Dim Stat As Long
   Dim J$
   
   Print "Hello World from Program " 
   Console_WaitKey
   Reg1()
   Console_WaitKey




function Reg1()


   Dim TheFile As String, FileData As String ' , H AS LONG
   Dim I As Long
   Dim J$
   ' REGEXPR, REREPL variables
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String


'  ****    REGEXPR demos   *****
   'PbHelpFile()                        ' Tested  OK
   'PhoneNumbers()                      ' tested OK
   'AlternateSpelling()                 ' tested OK
   'FindEachWordInString()              ' tested OK
    'FindAllComments_InSourceCode()      'tested OK
   'ThreewayAlternation()               ' tested OK
   'RegExprLineInput()                  ' tested OK




' 
   ' CompressSpaces()                    ' tested OK
    'ReplaceEachWordInStringWithPlural()  ' tested OK
    'ParseCSVFile()                      ' tested OK
    CreateDeclareFile()                 ' tested OK


end function


function PBHelpFile()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   TheMain = "Send your comments to BASICally Speaking at bsp@infoms.com"


   'Mask    = "[a-z_.-]+@[a-z_.-]+"
   Mask    = "[html]+@[html]+"
   dim nStart as long,s as string
   nStart=1
   Print mask
   'mask =chgRe(mask)
   'Print mask
   REGEXPR$(Mask, TheMain, startvar, PosVar, LenVar)
   'RegExpr Mask In TheMain To PosVar, LenVar
   Print "REGEXPR returns:" & Mid$(TheMain, PosVar, lenVar)
   Console_WaitKey
End Function  


function AlternateSpelling()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   
  Mask = "Colo[u]?r"      ' a 'u' in the indicated position is optional
  TheMain = " The color of the leaves is more brilliant than the colour of the television."


' INPUT: TheMain, Mask.  OUTPUT: multiple REGEXPR results
  PosVar = 1:   StartVar = 1
  While PosVar
    REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar) 
    'RegExpr Mask In TheMain At StartVar To PosVar,Lenvar
    If PosVar Then
         TheWord = Mid$(TheMain, Posvar, LenVar)
         Print TheWord
         StartVar = PosVar + LenVar
    End If
  Wend
end function


Function PhoneNumbers()
 Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
 Mask = "\([3 digits]\)-[3 digits]-[4 digits]"
 Print mask
 'mask=chgRe(mask)
 'Mask = "\([0-9][0-9][0-9]\)-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"
 TheMain = " call me at (900)-123-4567"
 REGEXPR$(Mask, TheMain, PosVar, LenVar)
 'RegExpr Mask In TheMain To PosVar,LenVar
 If PosVar Then Print Mid$(TheMain, PosVar, LenVar)
 Console_WaitKey
end function


function FindEachWordInString()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
 
   TheMain = " Now is the time for    all good men" & $CrLf & "to come to the aid of his party."
   Let mask = "[1+ letters][wordboundary]"        ' \b = word boundary. CRLF is (undocumented) word boundary
   Print mask
   'mask=chgRe(mask)
   'GoSub RegExprLoop
   PosVar = 1:   StartVar = 1
  While PosVar
    REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar) 
    'RegExpr Mask In TheMain At StartVar To PosVar,Lenvar
    If PosVar Then
         TheWord = Mid$(TheMain, Posvar, LenVar)
         Print TheWord
         StartVar = PosVar + LenVar
    End If
  Wend
end function


function ThreewayAlternation()
  Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String


  TheMain = " Two is too many to do the job "
  Mask = "(to[wordboundary])|(too[wordboundary])|(two[wordboundary])"     ' works fine 2, 9, 18 found
  Print mask
  'mask=chgRe(mask)


  Mask = "(to)|(too)|(two)\b"         ' did not find too(9), but did find to (18) and Two (2)  BUGBUGBUG
'  Mask = "((to)|(too)|(two))\b"        ' found Two (2) and to (18) but not too (9)
'  Mask = "[(to)|(too)|(two)]\b"       ' found single 'o' chararters at 4, 11, 19 and 22
'  Mask = "[(to)|(too)|(two)]+\b"      ' found Two(2), too(9), to (18) and 'o' at 22 (in 'do')
 '  GoSub RegExprLoop
 PosVar = 1:   StartVar = 1
  While PosVar
    REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar) 
    'RegExpr Mask In TheMain At StartVar To PosVar,Lenvar
    If PosVar Then
         TheWord = Mid$(TheMain, Posvar, LenVar)
         Print TheWord
         StartVar = PosVar + LenVar
    End If
  Wend
end function


function RegExprLineInput()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   Dim TheFile as string,FileData as string
   TheFile = "Regexpression.BAS"
   FileData = LoadFile (TheFile)
   'Print "Size of file is " & Str$(Len(FileData))
   Console_WaitKey
   StartVar = 1: PosVar   = 1
   Mask = "\n"                       ' \n = newline
   While PosVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      'RegExpr Mask In FileData At StartVar To PosVar, LenVar
      If PosVar Then
         TheWord = Mid$(FileData, StartVar, PosVar - StartVar-1)  ' NOTE: must remove the newline character
         Print "x";TheWord;"x"
         StartVar = PosVar + LenVar
      End If
   Wend
end function


function CompressSpaces()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   dim retFromReg as string
   
   TheMain = " The color   of the    leaves is more brilliant than the colour of     the television."
    Mask    = "\x20[\x20]+"         ' find space followed by one or more spaces
   'mask =chgRe(mask)
   Print mask + $crlf
    ReplaceMask = "\x20"          ' replace with a single space.
   PosVar = 1 :  StartVar = 1
   Print "TheMain Begins as " & $Dq & TheMain & $Dq + $crlf
   While StartVar
      REGEXPR$(Mask, TheMain, StartVar, PosVar, LenVar)
      retFromReg=Mid$(TheMain,PosVar,LenVar)
       
      'replaces multiple spaces with one space
      newMain = REPLACE$(TheMain, retFromReg, " ")


      'RegRepl Mask In TheMain With ReplaceMask At StartVar To Posvar, NewMain
      StartVar = PosVar
      TheMain = NewMain
   Wend
   Print "TheMain Converted to "  & $Dq & NewMain & $Dq + $crlf
end function


function ReplaceEachWordInStringWithplural()
   Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim Main As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain2 As String
   Dim ReplaceMask As String
   dim retFromReg as string
   dim newRetFromReg as string


   Main = "Now is the time for all good men" & $CrLf 
   'Find each word in a string      WORKS  9/17/99 even with embedded spaces, CRLF is an undocumented word boundary
    mask = "([A-Za-z]+)[\x20]"        ' not working
   Print mask + $crlf
   
   Main = "Now is the time for all good men" & $CrLf 
   print main
   ReplaceMask = "\01s"       ' for each word, append an 's'
   StartVar = 1: PosVar =1
   While StartVar
      REGEXPR$(Mask, Main, StartVar, PosVar, LenVar)
      retFromReg=Mid$(Main,PosVar,LenVar)
      
      newRetFromReg=trim$(retFromReg) + "s " 
      
      StartVar = PosVar+lenVar
      
      Main = REPLACE$(Main, retFromReg, newRetFromReg)
   Wend
   
   Print Main  + $crlf
end function


function ParseCSVFile()
  Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String
   dim FileData as string 


  ' file in:  "Value","value", "value",crlf, "value", "value", "value", Crlf.....
  FileData = $Dq & "Smith" & $Dq & "," & $Dq & "John"   & $Dq & "," & $Dq & "555-1234" & $Dq & $CrLf & _
             $Dq & "Doe"   & $Dq & "," & $Dq & "Jane"   & $Dq & "," & $Dq & "555-6789" & $Dq & $CrLf & _
             $Dq & "Brown" & $Dq & "," & $Dq & "Willie" & $Dq & "," & $Dq & "555-8765" & $Dq & $CrLf


  Dim WordMask As String


  Mask = "\q(.*)\q"
  Print mask + $crlf




  Print FileData + $crlf
  Print "*** END OF FILEDATA OF LEN=" & Str$(Len(FileData)) & "***" +$crlf


  Console_WaitKey
  dim retFromReg as string
  dim newRetFromReg as string
  PosVar = 1: StartVar = 1
  While StartVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      retFromReg=Mid$(Main,PosVar,LenVar)
      
      newRetFromReg=trim$(retFromReg)  
      
      StartVar = PosVar+lenVar
      
      Main = REPLACE$(Main, retFromReg, newRetFromReg)
   Wend
   
   Print Main  + $crlf  


  Print Filedata                        ' show the modified file
end function




function CreateDeclareFile()
  dim TheFile as string
  dim FileData as string
  
  Dim Mask As String, posvar As Long, lenvar As Long, startvar As Long
   Dim Mask2 As String
   Dim TheMain As String               ' 'MAIN' IS AN UNDOCUMENTED KEYWORD
   Dim TheWord As String
   Dim NewMain As String
   Dim ReplaceMask As String




  dim retFromReg as string
  dim newRetFromReg as string
  dim declaresFound as string
  TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
  FileData = File2Text(TheFile) 
  Mask = "^[\x20]*((SUB)|(FUNCTION))[\x20]+[a-z]+"   
   Print mask + $crlf
 
      ' first line of SUB/FUNCTIONs which use _ continuation to next line
  PosVar = 1
  Print "START OUTPUT for Mask = " & mask + $crlf
  
  PosVar = 1: StartVar = 1
  While StartVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      retFromReg=Mid$(FileData,PosVar,LenVar)
      declaresFound=declaresFound + "Declare "+retFromReg + $crlf
      
      StartVar = PosVar+lenVar
  Wend
'---------------------------------------------------------------------------------------
   Print declaresFound  + $crlf   








end function 


function FindAllComments_InSourceCode()
   dim TheFile as string
  dim FileData as string
  dim mask as string
  dim startvar as long
  dim posVar as long
  dim lenvar as long
  dim retFromReg as string
  TheFile = "C:\thinBasic\SampleScripts\regularExpression.tbasic"
  FileData = File2Text(TheFile) 
  dim comments as string
  Mask = "'[\x20]*[A-Za-z0-9 ]*"
  
  
  PosVar = 1: StartVar = 1
  While StartVar
      REGEXPR$(Mask, FileData, StartVar, PosVar, LenVar)
      retFromReg=Mid$(FileData,PosVar,LenVar)
      comments=comments + retFromReg + $crlf
      
      StartVar = PosVar+lenVar
  Wend
   
   Print comments  + $crlf   


end function




Function File2Text(sfn As String) As String


    Dim st  As String
    Dim n   As Long
     Dim FileHandle As DWORD,s as string
    FileHandle= FILE_OPEN(sfn, "INPUT")  
    while not FILE_EOF(FileHandle)
       s=FILE_LineInput(FileHandle)
       st=st+s+$crlf 
    wend
    
    s = FILE_Close(FileHandle)
    Function=st
End Function


  Function   bbb()  As Long
     ' dummy function, used in FindAllProceduresInSourceCode; indented on purpose
  End Function


Function ManyParameters (A As Single, B As Double, C As long, Z_A As String ) As Long
   ' dummy function for demo
End Function




'------------------