VEX String: add space after each 3 letters?

   759   6   1
User Avatar
Member
1074 posts
Joined: 4月 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
297 posts
Joined: 1月 2013
Offline
string s = "jghweiauhoigafawgeu";
string new_str = join(re_findall("[a-z]{1,3}", s), " ");
Edited by alexwheezy - 2024年10月30日 13:26:31
User Avatar
Member
4685 posts
Joined: 2月 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
2126 posts
Joined: 9月 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
1074 posts
Joined: 4月 2017
Offline
Fantastic!

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

-Olivier
Edited by olivierth - 2024年10月31日 11:05:41
User Avatar
Member
570 posts
Joined: 8月 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
1074 posts
Joined: 4月 2017
Offline
Ah! Thanks for the full explanation. I apriciate that!

-Olivier
  • Quick Links