coding_idiot
coding_idiot

Reputation: 13734

How to find all occurrences of $ which are not escaped or preceded by \ in java

Test Cases :

  1. he is having $money 500
  2. he is having \$money 500
  3. $
  4. \$

Matches should be the 1st & 3rd one, since 2nd & 4th already contains escaped "$".

I tried - Pattern pattern=Pattern.compile("(^\\\\$)"); but it doesn't match any. Please help.

Upvotes: 0

Views: 45

Answers (2)

hwnd
hwnd

Reputation: 70732

You can use Negative Lookbehind here to exclude the already escaped $ characters.

(?<!\\\\)\\$

Upvotes: 1

vks
vks

Reputation: 67968

(?<!\\)\$

You can try this.See demo.

http://regex101.com/r/sU3fA2/34

Upvotes: 3

Related Questions