VEX String: add space after each 3 letters?

   636   6   1
User Avatar
Member
1069 posts
Joined: April 2017
Offline
Hi!

Lets say I have a string:

jghweiauhoigafawgeu

But I want a space at every three letters like this:

jgh wei auh oig afa wge u

Any simple solution? My other option is a for loop but I'm curious to see a simpler way.

-Olivier
User Avatar
Member
296 posts
Joined: Jan. 2013
Offline
string s = "jghweiauhoigafawgeu";
string new_str = join(re_findall("[a-z]{1,3}", s), " ");
Edited by alexwheezy - Oct. 30, 2024 13:26:31
User Avatar
Member
4677 posts
Joined: Feb. 2012
Offline
Hi,

You can do this using regex easily:

string input = "jghweiauhoigafawgeu";
string result = join ( re_findall (".{1,3}", input ), " " );
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com]

youtube.com/@pragmaticvfx | patreon.com/animatrix | pragmaticvfx.gumroad.com
User Avatar
Member
2120 posts
Joined: Sept. 2015
Offline
Submitted - BUG #142040.

To review vex function web help page.

Update and review tags for all functions.

e.g. re_find() function - listed under strings section when filter drop down set to 'All tags' : But not listed under strings section when filter drop down set to strings(#of).
User Avatar
Member
1069 posts
Joined: April 2017
Offline
Fantastic!

Can you explain exactly what
[a-z]{1,3}
and
.{1,3}
do exactly?

-Olivier
Edited by olivierth - Oct. 31, 2024 11:05:41
User Avatar
Member
566 posts
Joined: Aug. 2014
Offline
[a-z]{1,3}
Matches any consecutive occurrences of characters from "a" to "z" range (case-sensitive) one to three times. It's greedy, meaning that it will keep on matching until it finds a character outside of a-z range, or it finds three consecutive occurrences of a-z characters. In both cases it will start a new matching operation.

jGhw eia-uhoigafawgeufs
---
0-1 j
2-4 hw
5-8 eia
9-12 uho
12-15 iga
15-18 faw
18-21 geu
21-23 fs

.{1,3}
This is the same as above, but operates on any character (.). Note that it will match whitespace.

jGhw eia-uhoigafawgeufs
---
0-3 jGh
3-6 w e
6-9 ia-
9-12 uho
12-15 iga
15-18 faw
18-21 geu
21-23 fs
User Avatar
Member
1069 posts
Joined: April 2017
Offline
Ah! Thanks for the full explanation. I apriciate that!

-Olivier
  • Quick Links