ReplaceRegex Help

I’m trying to output a formatted string, but struggling to find a clean formula.

If the input was:

Hello. I'm "___BOB___"! I'm feeling happy / healthy

I want the output to be:

hello_im_bob_im_feeling_happy_healthy

So basically:
All spaces should be replaced with underscores
All special characters (except for underscores) should be removed
There should never be more than 1 underscore in a row

I’ve got it mostly working with the following

Lower(
ReplaceRegex(
ReplaceRegex(
"Hello.  I'm "___BOB___"! I'm feeling happy / healthy", "[\s_]+", "_"),
"[^a-zA-Z0-9_]", "")
)

but it still spits out the following (based on my example):

hello_im__bob__im_feeling_happy__healthy

As you can see there’s a few spots that have double underscores.

How can I ensure the final output doesn’t have more than 1 underscore in a row? ChatGTP keeps giving me solutions that don’t seem to work in Fibery’s approved RegEx format.

Bonus points if it can all be done in a single ReplaceRegex function instead of needing to nest them!

You can’t do this with one regex, but I think the only change you need is

  • Add a “+” modifier to "[^a-zA-Z0-9_]"

Like this?

Lower(
ReplaceRegex(
ReplaceRegex(
“Hello. I’m “BOB”! I’m feeling happy / healthy”, “[\s_]+”, “"),
"[^a-zA-Z0-9
]+”, “”)
)

I still get

hello_im__bob__im_feeling_happy__healthy

I think I figured it out, just needed to essentially reverse my regex groups.

So first clear out all characters that are not spaces or underscores, then replace any spaces and underscores with a single underscore.

Lower(
  ReplaceRegex(
    ReplaceRegex(Hello. I'm "___BOB___"! I'm feeling happy / healthy, "[^a-zA-Z0-9_\s]+", ""),
    "[\s_]+",
    "_"
  )
)
1 Like