tags:

views:

43

answers:

1

String to be split

abc:def:ghi\:klm:nop

String should be split based on ":" "\" is escape character. So "\:" should not be treated as token.

split(":") gives

[abc]
[def]
[ghi\]
[klm]
[nop]

Required output is array of string

[abc]
[def]
[ghi\:klm]
[nop]

How can the \: be ignored

+5  A: 

Use a look-behind assertion:

split("(?<!\\\\):")

This will only match if there is no preceding \. Using double escaping \\\\ is required as one is required for the string declaration and one for the regular expression.

Gumbo