Friday, 30 August 2013

Java Regex replaceAll() with lookahead

Java Regex replaceAll() with lookahead

I am fairly new to using regex with java. My motive is to escape all
occurrences of '*' with a back slash. This was the statement that I tried:
String replacementStr= str.replaceAll("(?=\\[*])", "\\\\");
This does not seem to work though. After some amount of tinkering, found
out that this works though.
String replacementStr= str.replaceAll("(?=[]\\[*])", "\\\\");
Based on what I know of regular expressions, I thought '[]' represents an
empty character class. Am I missing something here? Can someone please
help me understand this?

No comments:

Post a Comment