Question: I want to replace a string, keeping the prefix, except when it contains a specific prefix. 

For instance, any string like `"(*)-bar"` must be replaced with `"(*)-blah"` except when `"(*)"` matches `"baz"`:

```
foo-bar => should return foo-blah 
baz-bar => should remain baz-bar

```

The best I have so far trims the last letter of the prefix when replacing:

```
echo "foo-bar" | sed s/"[^(baz)]-bar"/$1-blah/

```

Answer: 
Use *negative lookbehind*:

```
s/(?<!baz)-bar/-blah/

```

Most `sed` implementations don't have this advanced regexp feature, but it should work in more modern languages, such as perl.
With sed :

```
$ echo "foo-bar" | sed '/^foo-baz/!s/^foo-.*$/foo-blah/'
foo-blah
$ echo "foo-baz" | sed '/^foo-baz/!s/^foo-.*$/foo-blah/'
foo-baz

```

If I decompose : 

```
echo "foo-baz" | sed '/^foo-baz/!s/^foo-.*$/foo-blah/'
                      |        |||                   |
                      +  regex +|+ substitution part +
                                |
                              negation of regex

```