The reason a$ and i$ are getting removed is that
/[^\]$/ the regexp [^\\]\$ matches any character that is not '' followed by '$'
. You need to use zero width assertions
This is the same problem people have trying to find q not followed by u.
A first cut at the proper regexp is /(?<!\\)\$/ ( "(?<!\\\\)\\$" in java )
class Test { public static void main(String[] args) { String regexp = "(?<!\\\\)\\$"; System.out.println( java.util.Arrays.toString( "1a$1e\\$li$lo".split(regexp) ) ); } } Yields:
[1a, 1e\$li, lo]