Question: I have the following string in php:

```
$string = 'FEDCBA9876543210';

```

The string can be have 2 or more (I mean **more**) hexadecimal characters
I wanted to group string by 2 like :

```
$output_string = 'FE:DC:BA:98:76:54:32:10';

```

I wanted to use regex for that, I think I saw a way to do like "recursive regex" but I can't remember it.

Any help appreciated :)

Answer: 
If you don't need to check the content, there is no use for regex.

Try this

```
$outputString = chunk_split($string, 2, ":");
// generates: FE:DC:BA:98:76:54:32:10:

```

You might need to remove the last ":".

Or this :

```
$outputString = implode(":", str_split($string, 2));
// generates: FE:DC:BA:98:76:54:32:10

```

---

**Resources :**

* [www.w3schools.com - chunk\_split()](http://www.w3schools.com/php/func_string_chunk_split.asp)
* [www.w3schools.com - str\_split()](http://www.w3schools.com/php/func_string_str_split.asp)
* [www.w3schools.com - implode()](http://www.w3schools.com/php/func_string_implode.asp)

**On the same topic :**

* [Split string into equal parts using PHP](https://stackoverflow.com/questions/1794006/split-string-into-equal-parts-using-php)
Recursive regular expressions are usually not possible. You may use a regular expression recursively on the results of a previous regular expression, but most regular expression grammars will not allow recursivity. This is the main reason why regular expressions are almost always inadequate for parsing stuff like HTML. Anyways, what you need doesn't need any kind of recursivity.

What you want, simply, is to match a group multiple times. This is quite simple:

```
preg_match_all("/([a-z0-9]{2})+/i", $string, $matches);

```

This will fill `$matches` will all occurrences of two hexadecimal digits (in a case-insensitive way). To replace them, use preg\_replace:

```
echo preg_replace("/([a-z0-9]{2})/i", $string, '\1:');

```

There will probably be one ':' too much at the end, you can strip it with `substr`:

```
echo substr(preg_replace("/([a-z0-9]{2})/i", $string, '\1:'), 0, -1);

```