Will there be something like this work?
System.out.println( "1 duh 2 duh duh 3 duh" .replaceFirst("(duh.*?)duh", "$1bleh") ); // prints "1 duh 2 bleh duh 3 duh"
If you just want to replace the second occurrence of the pattern in a string, you really don't need this βstarting withβ index calculation.
As a bonus, if you want to replace all other duh (i.e. second, fourth, sixth, etc.), just call replaceAll instead of replaceFirst .
polygenelubricants
source share