Consider i have this String here :

```
[Hel[don't change in the middle]lo]

```

How can i replace the first and last bracket with another character like this :

```
#Hel[don't change in the middle]lo#

```

I know this is possible like this :

```
String str = "[Hel[don't change in the middle]lo]";
str = str.replaceFirst("\\[", "#");
if (str.substring(str.length() - 1).equals("]")) {
    str = str.substring(0, str.length() - 1) + "#";
    System.out.println(str);
}

```

> 
> Is there any other simple way using regex, so i can replace the first
>  and the last one in one shot?
> 
> 
> 

like this :

```
str = str.replace("myregex");

```