Date: 02-16-2022
Return to Index
created by gbSnippets
'This code handles these kinds of Boolean tests:
dog or cat
dog or cat or pig
my dog or your cat '2 search terms
dog and cat
dog and cat and pig
my dog and your cat '2 search terms
'This code does not handle combinations of AND/OR!
'Primary Code:
Sub BooleanSearch(ByVal sTerm$, ByVal sMain$, m$, iPos As Long, iResult As Long)
RegExpr " and " In sTerm$ To iResult
Do : RegRepl "( or )|( and )" In sTerm$ With ")|(" At iPos To iPos,sTerm$ : Loop While iPos
RegExpr "(" + IIf$(iResult, Left$(Repeat$(ParseCount(sTerm$,")|("), sTerm$ + ").*("),-4),sTerm$)+ ")" + m$ In sMain$ To iResult
End Sub
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Boolean Search",300,300,350,200, %WS_OverlappedWindow To hDlg
Control Add TextBox, hDlg, 300, "This can be the big string of words to search", 10,10,300,20
Control Add TextBox, hDlg, 400, "is and big", 10,40,300,20
Control Add CheckBox, hDlg, 500, "Exact Case Match", 10,70,200,20
Control Add Button, hDlg, 100,"Search", 50,100,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local sTerm$, sMain$, Match&, iResult&
Select Case Cb.Msg
Case %WM_InitDialog
PostMessage GetDlgItem(hDlg, 300), %EM_SETSEL, -1, 0
Case %WM_Command
Select Case Cb.Ctl
Case 100
Control Get Text hDlg, 400 To sTerm$
Control Get Text hDlg, 300 To sMain$
Control Get Check hDlg, 500 To Match&
BooleanSearch(sTerm$, sMain$, IIf$(Match&,"\c",""),1,iResult) 'iResult=0,no match iResult>0=match
Dialog Set Text hDlg, IIf$(iResult,"1","0")
End Select
End Select
End Function
Sub BooleanSearch(ByVal sTerm$, ByVal sMain$, m$, iPos As Long, iResult As Long)
RegExpr " and " In sTerm$ To iResult
Do : RegRepl "( or )|( and )" In sTerm$ With ")|(" At iPos To iPos,sTerm$ : Loop While iPos
RegExpr "(" + IIf$(iResult, Left$(Repeat$(ParseCount(sTerm$,")|("), sTerm$ + ").*("),-4),sTerm$)+ ")" + m$ In sMain$ To iResult
End Sub
'gbs_00836
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm