Using the code of a question I just answered as an example
Starting with the string
30-Nov-2012 30-Nov-2012 United Kingdom, 31-Oct-2012 31-Oct-2012 United
Arab Emirates, 29-Oct-2012 31-Oct-2012 India
What if we wanted to replace the space after every four-digit number with an @ such that you end up with something like this:
30-Nov-2012@30-Nov-2012@United Kingdom, 31-Oct-2012@31-Oct-2012@United
Arab Emirates, 29-Oct-2012@31-Oct-2012@India
How much more efficient is it to use a back-reference rather than a positive lookbehind (if at all)?
back-reference:
inputString.replaceAll("(\\d{4})\\s", "$1@");
positive lookbehind:
inputString.replaceAll("(?<=\\d{4})\\s", "@");
I admit my testing methodology is crude and may be flawed (furthermore I don’t know Java, only learning enough to write this answer), but my initial evidence proves contrary to @dasblinkenlight’s answer. I ran the following code:
…here, http://ideone.com/WkHLMN, and the output was:
Ignoring the first set of cases as outliers having to do with initialization, the remainder cases seem to indicate that the latter expression, using the positive lookbehind assertion, might do as much as 50% more work! I suspect this may be the case because backreferences would require going back to check whether an assertion is true, after passing the characters of interest.