grep implementation in Java