# Curiosity: unglobal first-only replace()?

**URL:** <https://discourse.julialang.org/t/curiosity-unglobal-first-only-replace/9006>\
**Category:** General Usage\
**Created:** [February 12, 2018, 6:19am UTC](https://discourse.julialang.org/t/curiosity-unglobal-first-only-replace/9006 "2018-02-12T06:19:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivo\_welch](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ivo\_welch](https://discourse.julialang.org/u/ivo_welch)\
**Post date:** [February 12, 2018, 6:19am UTC](https://discourse.julialang.org/t/curiosity-unglobal-first-only-replace/9006/1 "2018-02-12T06:19:03Z")

</div>

More a curiosity than a problem. julia’s replace is global. Perl requires the ‘g’ modifier to do a global replace.

```perl
my $s= "aabbccddaabbccddaabbccdd";
$s =~ s/(b+)/[$1]/;
print "$s\n";

```

replaces only the first match:

```julia
aa[bb]ccddaabbccddaabbccdd

```

Julia replaces all matches, because this is what replace does by default:

```julia
julia> replace("aabbccddaaabbbcccdddaabbccdd", r"(b+)", s"[\1]")
"aa[bb]ccddaaa[bbb]cccdddaa[bb]ccdd"

```

is there a built-in for first-only replacements, or do I program this?

/iaw

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 12, 2018, 6:50am UTC](https://discourse.julialang.org/t/curiosity-unglobal-first-only-replace/9006/2 "2018-02-12T06:50:45Z")

</div>

Julia v0.6:

```julia
julia> replace("aabbccddaaabbbcccdddaabbccdd", r"(b+)", s"[\1]", 1)
"aa[bb]ccddaaabbbcccdddaabbccdd"

```

Julia v0.7:

```julia
julia> replace("aabbccddaaabbbcccdddaabbccdd", r"(b+)" => s"[\1]", count = 1)
"aa[bb]ccddaaabbbcccdddaabbccdd"

```

It is also mentioned in the manual for replace (check `?replace` in the REPL).
