Tuesday, August 3, 2010

Egex-ray astardization-bay

A (loosely specified) Pig Latin regex translator:

String[] words = "ask another stupid question please".split(" ");

for (String word : words) {
   System.out.printf("%s -> %s%n", word,
      ("w" + word).replaceAll(
         "w(qu|[^aeiou]+|(?<=(w)))([a-z]*)",
         "$3-$1$2ay"
      )
   );
}
This prints (as seen on ideone.com)
ask -> ask-way
another -> another-way
stupid -> upid-stay
question -> estion-quay
please -> ease-play

No comments:

Post a Comment