Question: I have a query string that may look like one of the following:

1. ?key=aa=bb
2. ?key=aa=bb=cc
3. ?key=aa=bb&key2=cc

etc.

What I want to do is replace the equal sign in the value part only. So it should result in this:

1. ?key=aa%3dbb
2. ?key=aa%3dbb%3dcc
3. ?key=aa%3dbb&key2=cc

I'm trying to do that with the following regex by using a look ahead. But it's not doing anything.

```
echo preg_replace("/=(?=[^&])=/", "%3d", 'http://www.example.com?key=aaa=bbb=ccc&key3=dddd');

```

[Example code here](http://codepad.viper-7.com/qEBjtJ)

How can I make this work?

Answer: 
```
(\bkey\d*)=(*SKIP)(*F)|=

```

Try this.See demo.

<https://regex101.com/r/hR7tH4/13>

```
$re = "/(\\bkey\\d*)=(*SKIP)(*F)|=/m"; 
$str = "\n    ?key=aa=bb\n    ?key=aa=bb=cc\n    ?key=aa=bb&key2=cc\n"; 
$subst = "%3d"; 

$result = preg_replace($re, $subst, $str);

```
Here is another version of a regex based on the same approach as vks':

```
[&?][^&=]+=(*SKIP)(*FAIL)|=

```

**Regex explanation**:

* `[&?]` - Match `&` or `?` literally
* `[^&=]+` - Match characters other than `&` and `=`
* `=` - Match `=` (so, we matched a `key`)
* `(*SKIP)(*FAIL)` - Verbs that fail the match at this point (we do not replace this `=` we found after `key`)
* `=` - We match any other `=` and we'll remove it.

Here is [IDEONE demo](http://ideone.com/OzEu62):

```
$re = "/[&?][^&=]+=(*SKIP)(*FAIL)|=/"; 
$str = "http://google.com?key=aa=bb\nhttp://google.com?key=aa=bb=cc\nhttp://google.com?key=aa=bb&key2=cc"; 
$result = preg_replace($re, "%3d", $str);
echo $result;

```