Use negative lookbehind :
"(?<!\\\\):"
The reason for the four backslashes:
- The backslash is a special character in regular expressions, so you need the regular expression
\\ to match a single backslash. - backslashes must be escaped in Java strings, so each of the above backslashes must be written as
\\ , giving a total of four.
Code example:
Pattern pattern = Pattern.compile("(?<!\\\\):"); Matcher matcher = pattern.matcher("foo\\:x bar:y"); if (matcher.find()) { System.out.println(matcher.start()); }
Output:
10
Mark byers
source share