# Question about String - gsub

**URL:** <https://forum.crystal-lang.org/t/question-about-string-gsub/6937>\
**Category:** Help & Support\
**Created:** [June 16, 2024, 9:40pm UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937 "2024-06-16T21:40:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 16, 2024, 9:40pm UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937/1 "2024-06-16T21:40:31Z")

</div>

Hi guys, I have a specific need with **gsub** from the **String** class.

I would like when I perform a gsub on a string, when it match with my regex, to replace the matches with the match _prefixed_ by **\**.

This is the code I tried:

```crystal
puts ".salad./".gsub(/([.\/])/, "\\\\1")

```

But I got that result:

```bash
\1salad\1\1

```

I expect that result (to make you understand):

```bash
\.salad\.\/

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 16, 2024, 9:49pm UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937/2 "2024-06-16T21:49:21Z")

</div>

Is the goal here to essentially escape a string so it can be used in a regex?

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 16, 2024, 9:53pm UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937/3 "2024-06-16T21:53:02Z")

</div>

Basically, I provide an easy way to use sed command for the user, without thinking about the complexity of that command.

So when the user is calling that function, when I get the user entry, I would like to perform a test on that entry to escape all special characters sed can badly interpret.

It’s for that function:

```crystal
def replaceTextAllFilesRecursivelyNamed(path : String, filename : String, text : String, newText : String)
            regex = //
            replacement = ""

            requestedCommands = <<-CMD
                                find -name #{filename} -exec sed -i 's/#{text.gsub(regex,replacement)}/#{newText.gsub(regex,replacement)}/' {} \\;
                                CMD

            process = runSystemCommand(requestedCommands)

            if !process.success?
                Ism.notifyOfRunSystemCommandError(requestedCommands)
                Ism.exitProgram
            end
        end

```

Do you get what I try to do ?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 16, 2024, 9:53pm UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937/4 "2024-06-16T21:53:55Z")

</div>

If that’s the use case, I’d just run the string thru [Regex - Crystal 1.12.2](https://crystal-lang.org/api/Regex.html#escape%28str%29%3AString-class-method) and not try and do it yourself.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 16, 2024, 9:55pm UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937/5 "2024-06-16T21:55:12Z")

</div>

That is really magic 🥲 Thanks a lot my friend

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 18, 2024, 11:09am UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937/6 "2024-06-18T11:09:16Z")

</div>

I guess you add one more `\` in your replacement string. (if you prefer replace it yourself instead of use `Regex.escape`), you need double `\` to output the raw `\` in the string, and use %q to avoid explain the `\` as escaped char.

```crystal
puts ".salad./".gsub(/([\.\/])/, %q(\\\1))
# \.salad\.\/

```

BTW: Ruby use a more advanced regexp engine than Crystal, so, not all regexp expression work well on both langauge.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 21, 2024, 6:44pm UTC](https://forum.crystal-lang.org/t/question-about-string-gsub/6937/7 "2024-06-21T18:44:40Z")

</div>

I will probably to move to your solution, because I noticed on my last experiment that Regex.escape don’t do exactly what I want I just make the generated system unusable. I need to fix that
