s = "Thomas_J_Perkins"puts s.sub(/^(\p{L}+)(?:_\p{L}+)*_(\p{L})\p{L}*$/, "\\1_\\2")
Standard input is empty
Thomas_P
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!