Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
YoJae
Visitor

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 4
TheEndless
Channel Surfer

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
Visitor

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
Visitor

Re: roRegex count characters in given string

Thanks for your suggestion, this may do the trick 😉
0 Kudos
EnTerr
Roku Guru

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
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.