Date: 02-16-2022
Return to Index
created by gbSnippets
'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, "Test Code",300,300,230,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Find Next Whole Word - Instr", 20,10,180,25
Control Add Button, hDlg, 200,"Find Next Whole Word - RegExpr", 20,40,180,25
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local i,iPos,iLen As Long, sterm$, smain$
If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
smain$ = "own my own owner and brown own the own."
sterm$ = "own" : iPos = 1
Do
iPos = WholeWord_Instr(iPos,smain$,sterm$)
? Str$(iPos)
If iPos Then iPos = iPos + Len(sterm$)
Loop While iPos
End If
If Cb.Msg = %WM_Command And Cb.Ctl = 200 And Cb.CtlMsg = %BN_Clicked Then
smain$ = "Own my own owner and brown own the own."
sterm$ = "own" : iPos = 1 : iLen = 0
Do
iPos = iPos + iLen
RegExpr "\b" + sterm$ + "\b" In smain$ At iPos To iPos, iLen
? Str$(iPos)
Loop While iPos
End If
End Function
Function WholeWord_Instr(iPos As Long, smain$,sterm$) As Long
Local FlagLeft,FlagRight As Long, rChar$, lChar$, validcharacters$
'find sterm$
validcharacters$ = Chr$( 48 To 57, 65 To 90, 95, 97 To 122)
iPos = InStr(iPos, smain$, sterm$)
Do While iPos
FlagLeft = %False : FlagRight = %False
'check left side
If iPos = 1 Then
FlagLeft = %True 'flush with left side of line
Else
lChar$ = Mid$(smain$,iPos-1,1)
FlagLeft = IsTrue(Verify(lChar$, ValidCharacters))
End If
'check right side
If iPos = Len(smain$)-Len(sTerm$)+1 Then
FlagRight = %True 'flush with right side of line
Else
rChar$ = Mid$(smain$,iPos + Len(sterm$),1)
FlagRight = IsTrue(Verify(rChar$, ValidCharacters))
End If
'Done?
If FlagLeft And FlagRight Then Exit Do
iPos = InStr(iPos+Len(sterm$), smain$, sterm$)
Loop
Function = iPos
End Function
'gbs_01076
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm