Why not use regexp for this?

http://rubular.com/r/FbVfmDUIU6