# Replacing brackets in a string

**URL:** <https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478>\
**Category:** New to Julia\
**Tags:** question, strings, regex\
**Created:** [August 8, 2022, 1:59pm UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478 "2022-08-08T13:59:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)\
**Post date:** [August 8, 2022, 1:59pm UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/1 "2022-08-08T13:59:48Z")

</div>

I have a string such as `str = "MyFunction[x] = ..."`, where `x` is a value. How do I replace the square brackets with parentheses such that `str` contains `"MyFunction(x) = ..."`? There can be may different values `x`, and in my string there can also be other brackets that I don’t wish to replace (so I don’t wish to replace all occurrences, which I know how to do). Thanks.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 8, 2022, 2:14pm UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/2 "2022-08-08T14:14:17Z")

</div>

If your `x` isn’t going to contain the closing bracket `]` character, and these are going to be the first pair of brackets in your string, you can do either of the following:

```julia-repl
julia> replace(str, '[' => '(', ']' => ')', count = 2)
"MyFunction(x) = ..."

julia> replace(str, r"\[(.*?)\]" => s"(\1)", count = 1)
"MyFunction(x) = ..."

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 8, 2022, 2:39pm UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/3 "2022-08-08T14:39:21Z")

</div>

Not sure about the performance implications of replacing a longer string so benchmark if this is in a hot loop (although hopefully it isn’t!), but why not be explicit:

```julia
julia> replace(str, "MyFunction[x]" => "MyFunction(x)")
"MyFunction(x) = ..."

```

---

<div class="post-metadata">

**Author:** ![hellemo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hellemo/32/324_2.png) [@hellemo](https://discourse.julialang.org/u/hellemo)\
**Post date:** [August 8, 2022, 3:35pm UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/4 "2022-08-08T15:35:43Z")

</div>

Perhaps use Regex captures like this? [Strings · The Julia Language](https://docs.julialang.org/en/v1/manual/strings/#man-regex-literals)

```julia
julia> replace.(["MyFunction[x] = ...", "a = MyFunction[2]"], 
                 r"MyFunction\[(?<x>\w+)\]" => s"MyFunction(\g<x>)")
2-element Vector{String}:
 "MyFunction(x) = ..."
 "a = MyFunction(2)"

```

---

<div class="post-metadata">

**Author:** ![pywugate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pywugate/32/52011_2.png) [@pywugate](https://discourse.julialang.org/u/pywugate)\
**Post date:** [April 25, 2024, 10:00am UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/5 "2024-04-25T10:00:03Z")

</div>

Hi, I also have problem/question removing brackets “[” and “]”.  
I don’t fully understand the julia document page and PCRE2 page.

```julia
str1 = "['call', 'flight call']"
str2 = "['flight call']"
str1 * str2

```

I want the result like this `" 'call', 'flight call', 'flight call' "` .  
I tried many regex `replace(str1, regex => "")` :

```julia
1. r"\[(?<x>\w+)\]"
2. r"\[(?\w+)\]"
3. r"\[(?)\]"
4. r"\[(*)\]"
5. r"\[(\])"
6. r"(\[)(\])"
7. r"(\[)?\w+(\])"
8. r"(\[)?(\])"
9. r"(\[)*(\])"
10. r"(\[).(\])"

```

finally these 2 work ( but I feel they are not proper regex)

```julia
11. r"(\[)*(\])*")
12. r"(\[)?(\])?")

```

My question are

- why 6, 8, 9, 10 don’t work

```julia
6. r"(\[)(\])"
8. r"(\[)?(\])"
9. r"(\[)*(\])"
10. r"(\[).(\])" # PCRE2 said . for any character

```

- what is the better regex?

thanks!!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 25, 2024, 12:57pm UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/6 "2024-04-25T12:57:19Z")

</div>

> [@pywugate](#):
>
> My question are
> 
> - why 6, 8, 9, 10 don’t work

by modifying the 3rd and 4th patterns in the following way, the two square brackets are matched

```julia

ptn1=r"(\[)(\])"
ptn2=r"(\[)?(\])"
ptn3=r"(\[).*(\])"
ptn4=r"(\[).+(\])"  
julia> ptn3=r"(\[).*(\])"
r"(\[).*(\])"

julia> match(ptn3,str1)
RegexMatch("['call', 'flight call']", 1="[", 2="]") 

julia> ptn4=r"(\[).+(\])" # PCRE2 said . for any character
r"(\[).+(\])"

julia> match(ptn4,str1)
RegexMatch("['call', 'flight call']", 1="[", 2="]") 

```

---

<div class="post-metadata">

**Author:** ![pywugate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pywugate/32/52011_2.png) [@pywugate](https://discourse.julialang.org/u/pywugate)\
**Post date:** [April 27, 2024, 11:53am UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/7 "2024-04-27T11:53:16Z")

</div>

now I understand from your examples ptn3 and ptn4, thank you.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [April 27, 2024, 8:53pm UTC](https://discourse.julialang.org/t/replacing-brackets-in-a-string/85478/8 "2024-04-27T20:53:37Z")

</div>

People who end up in this discussion might find [ReadableRegex.jl](https://github.com/jkrumbiegel/ReadableRegex.jl) useful.
