
YoJae
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015
10:25 AM
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:
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
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
4 REPLIES 4

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015
10:49 AM
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)

RokuMarkn
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015
10:51 AM
Re: roRegex count characters in given string
It's probably simpler to just iterate through the characters rather than using roRegex for this.
--Mark
--Mark

YoJae
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015
10:51 AM
Re: roRegex count characters in given string
Thanks for your suggestion, this may do the trick 😉
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2015
11:27 AM
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 |"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.
BrightScript Debugger> ? len(s) - len(s.replace("a", ""))
7