'---Script created on 08-29-2023 20:39:59 by
uses "VBREGEXP"
Uses "Console"
uses "File"
dim text2 as String,mask as string
text2 = "Joe Smith , 160"
mask = "([A-Za-z]+)[ ]*([A-Za-z]+)[ ]*([, ]*)([0-9]*)"
print "Patient's First Name: " & vbregReplace(mask,text2,"$1") & $crlf
print "Patient's Second Name: " & vbregReplace(mask,text2,"$2") & $CRLF
Print "Patient's Weight: " & vbregReplace(mask,text2,"$4") & $crlf
print $CRLF
print "========================================" +$crlf
print $CRLF
mask = "([A-Za-z]+)[ ]*([A-Za-z]+)[ ]*,([ ]*)([0-9]*)" & $crlf
text2 = "What is this" & $crlf 'data errors
text2 += "Another Sentence" & $crlf 'data errors
text2 += "Joe Smith , 160" & $crlf
text2 += "Amy Smith , 110" & $crlf
text2 += "Clay Smith , 170" & $crlf
text2 += "Bob Smith , 160" & $crlf
text2 += "Steve Smith , 160" & $crlf
text2 += "Steve Smith Clay" & $crlf 'Regular Expression picks out values that are wrong
text2 += "Why is this here" & $crlf 'data mistakes
'Prints Comma Separated Values found
print vbregex(mask , 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 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
dim I as long
'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
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 = StrValue
end function
Bookmarks