正規表現でFizzBuzz
Pythonで対話的実行:
>>> MOD3 = re.compile("(([369][0369])|([147][258])|([258][147])|[^\d][369]) ") >>> MOD15 = re.compile("\d*[05]<") >>> MOD5 = re.compile("\d*[05] ") >>> MOD3AFTER = re.compile("\d+<") >>> data = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 " >>> MOD3.sub(r"\1<", data) '1 2 3<4 5 6<7 8 9<10 11 12<13 14 15<16 17 18<19 20 ' >>> MOD15.sub("FizzBuzz ", _) '1 2 3<4 5 6<7 8 9<10 11 12<13 14 FizzBuzz 16 17 18<19 20 ' >>> MOD5.sub("Buzz ", _) '1 2 3<4 Buzz 6<7 8 9<Buzz 11 12<13 14 FizzBuzz 16 17 18<19 Buzz ' >>> MOD3AFTER.sub("Fizz ", _) '1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz '