'---Script created on 08-29-2023 20:39:59 by
uses "VBREGEXP"
Uses "Console"
uses "File"
dim text2 as String,mask as string
Dim variables as string,quotes as string, typeOfVar as String
Dim parameter as string,MultipleParameters as String
Dim func as string ,frontPar as string,backPar as String
dim funcName as string
frontPar = "[ ]*\([ ]*" 'front parenthesis (
backPar = "[ ]*\)[ ]*" 'back parenthesis )
funcName = "[ ]*function[ ]*"
'note: [ ]* is zero of more spaces
'variables start with letter next is letter or numbers or underscore ex... a_12 van3 va33n not 1van
Variables = "([ ]*[a-zA-Z][a-zA-Z0-9_]*[ ]*)*"
quotes = "[ ]*[\x34](.*)[\x34][ ]*"
typeOfVar = "([ ]*(as)[ ]*(long|string)[ ]*)*" ' as string or as long
Parameter = Variables + typeOfVar 'the parameter either long or string
MultipleParameters = "([ ]*,[ ]*"+Parameter+")*" 'Multiple parameters
'regular expression patterns tend to be long and complex so break them up and test as you go
func=funcName + variables + FrontPar + Parameter + MultipleParameters + backPar + typeofvar
text2 = "function " & $crlf 'data errors no function Name or no parenthesis
text2 += "function bbb " & $crlf 'data errors no parenthesis
text2 += "function bbb (" & $crlf 'data error Miss parenthesis
text2 += "function bbb ( )" & $crlf
text2 += "function bbb (aa as string , bb as long , cc as string) as string" & $crlf
text2 += "function aaa()" & $crlf
text2 += "function bbb (aa as string)" & $crlf
text2 += "function bbb (aa as string , bb as long , cc as string) as long" & $crlf
text2 += "function bbb (aa as string , bb as long )" & $crlf
text2 += "function bbb (aa as string , bb as long , cc as string) as string" & $crlf
print vbregex(func , text2 ) + $CRLF
print "========================================" +$crlf
Console_WaitKey
'returns one value that pattern matched and replaced
function vbregReplace(pattern as string,txt as string,replaceTxt as string ) as string 'returns the Value that was match and replaced
'pattern is a Visual Basic regular expression pattern
'replaceTxt is the text that is used in replacement $1 $2
'$1$2$3 is the patterns that are surrounded by parenthesis ([A-Za-z]+)[ ]*([0-9]+)
'[classes] (sub-patterns)
'[a-z] all lower case [A-Z] all upper case [0-9] any 0123456789 number
' using $1$2$3 you can separate the part of the pattern you want returned and in which order
'VBREGEXP_SetPattern lpRegExp, "(\S+)(\s+)(\S+)"
'strRetVal = VbRegExp_Replace(lpRegExp, txt, "$3$2$1")
'third pattern found would come first second pattern then first pattern
dim lpRegExp as dword
'dim txt as string value "The quick brown fox jumped over the lazy dog."
dim strRetVal as string
'---Allocate a new regular expression instance
lpRegExp = VBREGEXP_New
'---Check if it was possible to allocate and if not stop the script
if isfalse lpRegExp then
print "Unable to create an instance of the RegExp object." & $crlf & "Script terminated"
stop
end if
'---Set case insensitivity
VBREGEXP_SetIgnoreCase lpRegExp, -1
'---Set global applicability
VBREGEXP_SetGlobal lpRegExp, -1
'---Replace example 1
'---Replace 'fox' with 'cat'.
VBREGEXP_SetPattern lpRegExp, pattern
strRetVal = VbRegExp_Replace(lpRegExp, txt, replaceTxt )
'---Replace example 2
'---In addition, the Replace method can replace subexpressions in the pattern.
'---The following swaps the first pair of words in the original string:
'VBREGEXP_SetPattern lpRegExp, "(\S+)(\s+)(\S+)"
'strRetVal = VbRegExp_Replace(lpRegExp, txt, "$3$2$1")
'print strRetVal+$crlf
'---Deallocate regular expression resource
IF istrue lpRegExp THEN VBREGEXP_Release(lpRegExp)
'returns the Value that was match and replaced
function = strRetVal
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
'Makes a Comma Separated Value list of the patterns found
function vbregex(pattern as string, txt as string ) as string 'returns patterns found
'pattern is VisualBasic regular expression pattern
'txt is the text to be evaluated with the vbregex regular expression
dim I as long ,allStr as string
dim lpRegExp as dword
dim lpMatches as dword
dim lpMatch as dword
dim strValue as string
'---Allocate a new regular expression instance
lpRegExp = VBREGEXP_New
'---Check if it was possible to allocate and if not stop the script
if isfalse lpRegExp then
Print "Unable to create an instance of the RegExp object." & $crlf & "Script terminated" + $crlf
stop
end if
'---Set pattern
VBREGEXP_SetPattern lpRegExp, pattern '"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"
'---Set case insensitivity
VBREGEXP_SetIgnoreCase lpRegExp, -1
'---Set global applicability
VBREGEXP_SetGlobal lpRegExp, -1
'---Execute search '"Please send a mail to eros.olmi@thinbasic.com or to <support@thinbasic.com>. Thanks!"
lpMatches = VBREGEXP_Execute(lpRegExp, txt)
IF ISFALSE lpMatches THEN
Print "1. No match found"
else
dim nCount as long value VBMatchCollection_GetCount(lpMatches)
IF nCount = 0 THEN
Print "2. No match found" + $crlf
else
'---Iterate the Matches collection
'strValue += "match number,position,getLength,patternMatched" & $crlf
FOR i = 1 TO nCount
lpMatch = VBMatchCollection_GetItem(lpMatches, i)
IF ISFALSE lpMatch THEN EXIT FOR
'strValue += i & "," & VBMatch_GetFirstIndex(lpMatch) & "," & VBMatch_Getlength(lpMatch) & "," & VBMatch_GetValue(lpMatch) & $CRLF
strValue = VBMatch_GetValue(lpMatch) ' & $CRLF
allStr = allStr + StrValue + $crlf
VBREGEXP_Release lpMatch
NEXT
'Print strValue + $crlf
END IF
END IF
IF istrue lpMatches THEN VBREGEXP_Release(lpMatches)
IF istrue lpRegExp THEN VBREGEXP_Release(lpRegExp)
'matches of pattern
function = allstr
end function
Bookmarks