Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
YoJae
Level 7

roRegex count characters in given string

Hi guys,

I am struggling with regular expression to count how many characters does a string contains.

I am using Match command which should output an array of characters.
These are my commands in the debugger:

BrightScript Debugger> ra = createObject("roRegex", "(a)", "")
BrightScript Debugger> ? ra.Match("ha |fg d a a aa a a |")
a
a


As you can see it only outputs 2 "a" characters and an empty character.
My idea was to get count() of the array.

How can I match characters or is there any better way for this?
Many thanks
0 Kudos
4 Replies
TheEndless
Level 7

Re: roRegex count characters in given string

Unfortunately, the regex implementation on the Roku only supports finding the first match in a string. There's no way that I'm aware of to find all matches. You could potentially use roRegex.Split to split the string into an array and count the entries, but that may drop empty strings.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
RokuMarkn
Level 7

Re: roRegex count characters in given string

It's probably simpler to just iterate through the characters rather than using roRegex for this.

--Mark
0 Kudos
YoJae
Level 7

Re: roRegex count characters in given string

Thanks for your suggestion, this may do the trick Smiley Wink
0 Kudos
EnTerr
Level 8

Re: roRegex count characters in given string

"TheEndless" wrote:
You could potentially use roRegex.Split to split the string into an array and count the entries, but that may drop empty strings.

roRegEx.split() does not drop empty strings. So it will work but is brutal. Here is a slightly better way:
BrightScript Debugger> s = "ha |fg d a a aa a a |"
BrightScript Debugger> ? len(s) - len(s.replace("a", ""))
7
roRegEx.replaceAll() can do the same for a pattern - or if multiple stats are to be accounted, just roll over the string and collect in a dictionary.
0 Kudos