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)
Sounds like you want a regex like this:

```
/([0-9a-f]{2})/${1}:/gi

```

Which, in PHP is...

```
<?php
$string = 'FE:DC:BA:98:76:54:32:10';
$pattern = '/([0-9A-F]{2})/gi';
$replacement = '${1}:';
echo preg_replace($pattern, $replacement, $string);
?>

```

Please note the above code is currently untested.