# Put an underscore before all digits in a string

**URL:** <https://discourse.julialang.org/t/put-an-underscore-before-all-digits-in-a-string/50195>\
**Category:** General Usage\
**Tags:** question, strings\
**Created:** [November 15, 2020, 6:05pm UTC](https://discourse.julialang.org/t/put-an-underscore-before-all-digits-in-a-string/50195 "2020-11-15T18:05:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [November 15, 2020, 6:05pm UTC](https://discourse.julialang.org/t/put-an-underscore-before-all-digits-in-a-string/50195/1 "2020-11-15T18:05:01Z")

</div>

I’ve tried reading these docs [Introducing Julia/Strings and characters - Wikibooks, open books for an open world](https://en.wikibooks.org/wiki/Introducing_Julia/Strings_and_characters) and these docs [Strings · The Julia Language](https://docs.julialang.org/en/v1/base/strings/#Base.@s_str) and I’ve tried several things, such as:

```julia
replace("x1", r"\d+" => s"_\1")

```

which returns:

```julia
PCRE error: unknown substring

```

. And:

```julia
replace("x1", r"[0-9]+" => s"_\1")

```

which returns:

```julia
PCRE error: unknown substring

```

. I’ve also tried:

```julia
replace("x1", r"[0-9]+" => s"_\g")

```

which returns:

```julia
Bad replacement string: _\g

```

. And:

```julia
replace("x1", r"\d+" => SubstitutionString("_\\1"))

```

which returns:

```julia
PCRE error: unknown substring

```

. And:

```julia
replace("x1", r"\d+" => SubstitutionString("_\\g"))

```

which returns:

```julia
Bad replacement string: _\g

```

Must admit I’m kind of stumped for what to do. Any ideas?

In case my question is unclear, what I’m expecting is the output “x\_1”.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [November 15, 2020, 6:13pm UTC](https://discourse.julialang.org/t/put-an-underscore-before-all-digits-in-a-string/50195/2 "2020-11-15T18:13:05Z")

</div>

I think what you’re missing is that `\1` refers to the first capture in the regex match, and to create a capture you need to use parentheses in the regex:

```julia
julia> replace("x123y456", r"(\d+)" => s"_\1")
"x_123y_456"

```

(note the parentheses around `\d+`)
