CountX

Share your advanced PureBasic knowledge/code with the community.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

CountX

Post by applePi »

not a big discovery, but inspired by count, countx functions in basic-256 interpreter https://sourceforge.net/projects/kidbasic/
here we CountX the number of 'bb' not preceded by x Or z
using an already available example from PureBasic https://www.purebasic.com/documentation ... ssion.html i just want to give you the idea of simple syntax to find things according to a pattern.

Code: Select all

Procedure.l CountX(text.s, regex.s)
  If CreateRegularExpression(0, regex)
    If ExamineRegularExpression(0, text)
      While NextRegularExpressionMatch(0)
        Debug "Match: " + RegularExpressionMatchString(0)
        Debug "    Position: " + Str(RegularExpressionMatchPosition(0))
        Debug "    Length: " + Str(RegularExpressionMatchLength(0))
        t+1
      Wend
    EndIf
  Else
    Debug RegularExpressionError()
  EndIf
  ProcedureReturn t
EndProcedure

text.s = "xbbc Abbc zbbA abxbbc to bebb or not to xebbe"
regex.s = "[^xz]bb"
  
result = CountX(text, regex)
Debug #CRLF$ + "Number of matches = "+Str(result)
i think the same can be done in purebasic with other functions in basic 256:
countx ( haystack_string_expression , regular_expression )
instrx ( haystack_string_expression , regular_expression )
midx ( haystack_string_expression , regular_expression )
variable = explodex ( string_expression , regular_expression )
implode(....)
replacex ( haystack_string_expression , regular_expression , string_expression )

don't think i'm making promotion to basic 256, in fact its very limited , but i find it very strange to add regular expressions to a basic taught in elementary schools !!
these RE functions are inserted lately and silently to the language