i have tried a regular expressions engine (perl compatible) with darkbasic which can be found in codeproject
here...
another place for C++ coders
here...
from the codeproject you can download a DLL which i will use here with darkbasic.
at first some search suggestions: suppose we want to search an alien DNA "wz12xy534562rs339tre" for the sequence "2\w\w\d3" which means :" digit 2, any char but not a digit, any char but not a digit, any digit, digit 3 "
another pattern wanted may be the sequence "2.*?3" which means: digit 2 followed by any char or digit then followed by digit 3" and we use "?" to limit the findings to the minimum groups and not the widest one.
after a ton of trials it works for me, to use mid$(string,x,y) use Matrix1Util_16 from here
http://forum.thegamecreators.com/?m=forum_view&t=85209&b=18
http://www.matrix1.demon.co.uk/Matrix1Utils_Help/Matrix1_Index.html
those are a great utilities and i think must be part of darkbasic
the following code i have attached here:
http://www.mediafire.com/download.php?q4gar4xavpibbbc
together with the libdeelx.dll regular expressions engine
1- search for a pattern ""2\w\w\d3" :
sync off
load dll "libdeelx.dll",1
if DLL EXIST(1)=1
set text size 20
SET TEXT TO BOLD
txt$ = "wz12xy534562rs339tre"
regex$ = "2\w\w\d3"
rem regex$ = "2.*?3"
print "using the pattern : " + regex$
print "over the text : " + txt$
print "produce the following results :"
startpos = -1
handle=CALL DLL(1,"regexp_create")
result=CALL DLL(1,"result_create")
CALL DLL 1,"regexp_compile", handle, regex$, 0
For i = 1 To Len(txt$)
CALL DLL 1,"regexp_match", handle, txt$, startpos,result
rs = CALL DLL(1,"result_ismatched",result)
If rs <> 0
resultStart=CALL DLL(1,"result_start",result)
resultEnd=CALL DLL(1,"result_end",result)
print "DLL return value is : "+str$(result)
print "pattern start : "+str$(resultStart+1)
print "pattern end : "+str$(resultEnd)
print mid$(txt$,resultStart+1,resultEnd-resultStart)
startpos = resultEnd
else
exit
endif
next
DELETE DLL 1
else
print "no dll found"
endif
wait key
end
2- the following code replace cat with Lion from a file testfile.txt and output the result to a.txt
sync off
load dll "libdeelx.dll",1
set text size 20
handle=CALL DLL(1,"regexp_create")
CALL DLL 1,"regexp_compile", handle, "cat", 0 `regex rule
open to read 1,"testfile.txt"
open to write 2, "a.txt"
Do
read string 1,txt$
if txt$="" then exit
result_length = CALL DLL(1,"regexp_replace", handle, txt$, "cat", -1,-1, "",0)
result$=space$(result_length*2)
CALL DLL 1,"regexp_replace", handle, txt$, "Lion" ,-1,-1,result$, result_length*2
print txt$
print "the new text is : "
print result$
write string 2, result$
loop
DELETE DLL 1
close file 1
close file 2
wait key
end
in the replace example i am not sure of the line "
result$=space$(result_length*2)"
i will be glad for any modifications, or correcting. i find it strange that darkbasic don't need to declare previously the functions inside a DLL.
i attach also a vb6 project to compare.
download the projects above from
here....