Code: Select all
Debug FindString("text1$ text2$ text3$","t2$") ; Want 0 returned, not 11.
Code: Select all
Debug FindString("text1$ text2$ text3$","t2$") ; Want 0 returned, not 11.
Code: Select all
EnableExplicit
Procedure FindString2(Source.s, SearchString.s)
Protected pos = 1
Protected length = Len(SearchString)
Protected Result, charStart, charEnd
Repeat
pos = FindString(Source, SearchString, pos)
If pos
charStart = Asc(Mid(Source, pos - 1, 1))
charEnd = Asc(Mid(Source, pos + length, 1))
; Debug Chr(charStart)
; Debug Chr(charEnd)
; Debug pos
; If Not ((charStart >= 65 And charStart <= 90) Or (charStart >= 97 And charStart <= 122) Or (charStart >= 48 And charStart <= 57 Or charStart = 36) Or (charEnd >= 65 And charEnd <= 90) Or (charEnd >= 97 And charEnd <= 122) Or (charEnd >= 48 And charEnd <= 57) Or charEnd = 36)
If (Not ((charStart >= 65 And charStart <= 90) Or (charStart >= 97 And charStart <= 122) Or (charStart >= 48 And charStart <= 57 Or charStart = 36)) Or pos = 1) And Not ((charEnd >= 65 And charEnd <= 90) Or (charEnd >= 97 And charEnd <= 122) Or (charEnd >= 48 And charEnd <= 57) Or charEnd = 36)
Result = pos
Break
EndIf
pos + 1
Else
Break
EndIf
ForEver
ProcedureReturn Result
EndProcedure
Debug FindString2("text1$ text2$ ext text3$","ext")
Debug FindString2("t2$ text1$ text2$ text3$ t2$","t2$")
Code: Select all
EnableExplicit
Procedure FindString2(Source.s, SearchString.s, separator.s = #TAB$ + " .,:<>()[]{}!")
Protected pos = 1
Protected length = Len(SearchString)
Protected Result, charStart.s, charEnd.s
Repeat
pos = FindString(Source, SearchString, pos)
If pos
charStart = Mid(Source, pos - 1, 1)
charEnd = Mid(Source, pos + length, 1)
; Debug charStart
; Debug charEnd
; Debug pos
; If FindString(separator, charStart) And FindString(separator, charEnd)
If (pos = 1 Or FindString(separator, charStart)) And (FindString(separator, charEnd) Or charEnd = "")
Result = pos
Break
EndIf
pos + 1
Else
Break
EndIf
ForEver
ProcedureReturn Result
EndProcedure
Debug FindString2("text1$ text2$ text3$","t2$")
Debug FindString2("text1$ text2$ text3$ t2$","t2$")
Code: Select all
EnableExplicit
Procedure FindString2(Source.s, SearchString.s)
Protected pos = 1
Protected length = Len(SearchString)
Protected Result, charStart, charEnd
Repeat
pos = FindString(Source, SearchString, pos)
If pos
charStart = Asc(Mid(Source, pos - 1, 1))
charEnd = Asc(Mid(Source, pos + length, 1))
If pos = 1
charStart = 1
Else
Select charStart
Case 65 To 90, 97 To 122, 48 To 57, 36
pos + 1
Continue
EndSelect
EndIf
Select charEnd
Case 65 To 90, 97 To 122, 48 To 57, 36
pos + 1
Continue
EndSelect
Result = pos
Break
Else
Break
EndIf
ForEver
ProcedureReturn Result
EndProcedure
Debug FindString2("text1$ text2$ ext text3$","ext")
Debug FindString2("t2$ text1$ text2$ text3$ t2$","t2$")
Code: Select all
Debug FindString("text1$ text2$ text3$"," t2$") ; returns 0
Code: Select all
Procedure FindStringWhole(textString$, toFind$)
Protected count = Len(toFind$)
Protected count2 = count + 1
Protected pos = 0
If textString$ = toFind$ Or Left(textString$, count2) = toFind$ + " "
pos = 1
ElseIf Right(textString$, count2) = " " + toFind$
pos = Len(textString$) - count+1
Else
pos = FindString(textString$, " " + toFind$ + " ")
If pos > 0
pos + 1
EndIf
EndIf
ProcedureReturn pos
EndProcedure
Debug FindStringWhole("text1$ text2$ text3$","t2$")
Code: Select all
Procedure FindWholeWord(Source.s, SearchString.s, Mode.i = 0)
Protected regExFlags.i = #PB_RegularExpression_MultiLine
If Mode = #PB_String_NoCase
regExFlags | #PB_RegularExpression_NoCase
EndIf
Protected hRegEx.i = CreateRegularExpression(#PB_Any, "\b" + SearchString + "\b", regExFlags)
If Not hRegEx
ProcedureReturn -1
EndIf
If Not ExamineRegularExpression(hRegEx, Source)
ProcedureReturn -2
EndIf
Protected position.i
If NextRegularExpressionMatch(hRegEx)
position = RegularExpressionMatchPosition(hRegEx)
EndIf
FreeRegularExpression(hRegEx)
ProcedureReturn position
EndProcedure
Debug FindWholeWord("text1$ text2$ text3$", "t2$")
Debug FindWholeWord("Hello World!", "world", #PB_String_NoCase)
Adding parameters can slow down the function.BarryG wrote: Wed May 14, 2025 12:47 pm Can FindString maybe be updated with a flag to only find whole words? Like this:
This sounds false to me.AZJIO wrote: Wed May 14, 2025 11:23 pm Adding parameters can slow down the function.
If only the compiler counted the number of function parameters and chose a more optimized one. For example, if there are 4 parameters, then the FindString4 function is embedded, and if there are 5 parameters, then the FindString5 function is embedded, that is, as the parameters increase, the compiler would embed the function with the largest number of parameters, while the function name remains FindString. Then increasing the number of parameters would not harm those who want a fast function with fewer parameters. If there are many function calls with different parameters, then the only function with the largest number of parameters is embedded.
Oh, then Purebasic's RegEx engine seems to be configured badly. Usually it works with UTF-8 characters and not just latin characters.AZJIO wrote: Wed May 14, 2025 6:38 pm \b - works only for Latin letters and numbers and _, but is not suitable for $.
Exactly. That's what I said.AZJIO wrote: Wed May 14, 2025 6:38 pm The string may contain meta characters and they must be escaped.
I don't think a lot of people care about the executable size today.
I'm a huge proponent of executable size, trying to make my binaries as small as they can reasonably be.NicTheQuick wrote: Thu May 15, 2025 2:16 pmI don't think a lot of people care about the executable size today.
I,ve seen a monster executable created by python over a gig... :SQuin wrote: Thu May 15, 2025 2:40 pm Ever tried using regular expressions in Python or .NET and then seeing all the stuff it has to add to your dist?
Sheesh!
Well, you can not compare a language that gets compiled into an executable with a script language that needs a VM. Also regular expressions are one of the included libraries of Python. So if you use it or not, it has always the same size. And I don't know anybody who compiles Python into an executable. Maybe some Windows guys do weird things like that. On Linux I never saw that. It compiles itself into bytecode but that's all.Quin wrote: Thu May 15, 2025 2:40 pm So, even if you care about binary size, PB is the best choice. Ever tried using regular expressions in Python or .NET and then seeing all the stuff it has to add to your dist?