Saturday, May 22, 2010

I don't like regex! I love it!

System.out.println("du hast mich".replaceAll("(?<=^(.*)) ", ", $1 "));
// prints "du, du hast, du hast mich"

System.out.println("hold thrill kiss kill me".replaceAll("(?= .* (.*))", " $1,"));
// prints "hold me, thrill me, kiss me, kill me"

System.out.println("Mmm ".replaceAll(".(?=.*?( )?(?<=(^.*) ))", "$2$1"));
// prints "Mmm Mmm Mmm Mmm"

No comments:

Post a Comment