I had confusion over lookahead and lookback regular expression operator. This was clarified once I read the book and tried some examples in scala.
Lookahead/Lookback operator are used to identify position in target string from where we need to start matching. These are same as ^ or $ which is used to define start and end position of a line. ^ and $ are a specific example of these operators.
Let’s see these by example:
Create a pattern with regular expression. Here ?= is a lookforward operator. It says that try to find a string “Vivek” and set the pointer(Assume location to be pointer) at the beginning of the match.
Then from that position look for a string VivekKumar.
scala> val r = new Regex("(?=Vivek)VivekKumar")
r: scala.util.matching.Regex = (?=Vivek)VivekKumar
res31: String = VivekKumar
res32: String = ""
In last example pointer is at the beginning (V) and then system tries to match VivekKumar and it will not match.
Lookback is same as lookahead but instead of setting pointer at the beginning it will set pointer at the end of the matched word.
Here ?<= is lookback operator.
scala> val r = new Regex("(?<=Vivek)AbhishekKumar")
r: scala.util.matching.Regex = (?<=Vivek)AbhishekKumar
res35: String = AbhishekKumar
After matching the lookback operator pointer points to k(5th character) VivekAbhishekKumar, from then it will try to match next expression(AbhishekKumar) hence it prints AbhishekKumar.