Search code examples
phpregexpreg-split

Regex to ignore certain occurrences


I have a rather sticky little regex issue I could use some help with.

I have a source string that will look like this;

%t:H\:i\:s

What I am struggling with is trying to figure out what the proper regex statement would be that I can use in a PHP preg_split function that will split the sample string above on the standalone colon ':', but leave the ':' string literal specified sequences out of the split.

So, when successfully completed, the end result would be;

array
(
  [0] => %t
  [1] => H\:i\:s
)

I'm no guru with regex and have been trying numerous strategies, with no luck yet. Any help would be immensely appreciated.

Thanks, in advance.


Solution

  • You may use negative lookbehind: preg_split("/(?<!\\\\):/", "%t:H\:i\:s")

    DEMO