Skip to main content
formatting
Source Link
KitsuneYMG
  • 12.9k
  • 5
  • 39
  • 58

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]

The reason a$ and i$ are getting removed is that

/[^\]$/ matches any character that is not '' followed by '$'

  You need to use zero width assertions

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]

Source Link
KitsuneYMG
  • 12.9k
  • 5
  • 39
  • 58

The reason a$ and i$ are getting removed is that

/[^\]$/ matches any character that is not '' followed by '$'

You need to use zero width assertions