文字列が空行か調べる

改行だけの行の場合

以下の2つを組み合わせる

^ 	行の先頭
$ 	行の末尾
groovy:000> (~/^$/).matcher("a\n").find()
===> false
groovy:000> (~/^$/).matcher("\n").find()
===> true
groovy:000> (~/^$/).matcher("a").find()
===> false
groovy:000> (~/^$/).matcher("").find()
===> true
groovy:000> (~/^$/).matcher("\t\n").find()
===> false
groovy:000> (~/^$/).matcher("aaa\nbbb").find()
===> false

空白も入っていても空行とする場合

以下を組み合わせる

\A 	入力の先頭
\s 	空白文字:[ \t\n\x0B\f\r]
\z 	入力の末尾
*       0 回以上
groovy:000> (~/\A\s*\z/).matcher("a\n").find()
===> false
groovy:000> (~/\A\s*\z/).matcher("\n").find()
===> true
groovy:000> (~/\A\s*\z/).matcher("a").find()
===> false
groovy:000> (~/\A\s*\z/).matcher("").find()
===> true
groovy:000> (~/\A\s*\z/).matcher("\t\n").find()
===> true
groovy:000> (~/\A\s*\z/).matcher("aaa\nbbb").find()
===> false

実行環境