You need to use a negative lookbehind statement :
Pattern regex = Pattern.compile( "(?<! # Make sure there is no... \n" + " &\\# # &#, followed by \n" + " [0-9A-F]{0,3} # zero to three hex digits \n" + ") # right before the current position. \n" + "\\d{3} # Only then match three digits.", Pattern.COMMENTS);
You can use it as follows:
Matcher regexMatcher = regex.matcher(subjectString); return regexMatcher.find(); // returns True if regex matches, else False
source share