# ReplaceRegex Help

**URL:** <https://community.fibery.io/t/replaceregex-help/6430>\
**Category:** Get Help\
**Created:** [July 24, 2024, 1:55pm UTC](https://community.fibery.io/t/replaceregex-help/6430 "2024-07-24T13:55:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![interr0bangr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/interr0bangr/32/8089_2.png) [@interr0bangr](https://community.fibery.io/u/interr0bangr)\
**Post date:** [July 24, 2024, 1:55pm UTC](https://community.fibery.io/t/replaceregex-help/6430/1 "2024-07-24T13:55:50Z")

</div>

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [July 24, 2024, 2:06pm UTC](https://community.fibery.io/t/replaceregex-help/6430/2 "2024-07-24T14:06:04Z")

</div>

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_]"`

---

<div class="post-metadata">

**Author:** ![interr0bangr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/interr0bangr/32/8089_2.png) [@interr0bangr](https://community.fibery.io/u/interr0bangr)\
**Post date:** [July 24, 2024, 2:13pm UTC](https://community.fibery.io/t/replaceregex-help/6430/3 "2024-07-24T14:13:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![interr0bangr](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/interr0bangr/32/8089_2.png) [@interr0bangr](https://community.fibery.io/u/interr0bangr)\
**Post date:** [July 24, 2024, 3:47pm UTC](https://community.fibery.io/t/replaceregex-help/6430/4 "2024-07-24T15:47:50Z")

</div>

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.

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

```
