Ruby String split with regex

前端 未结 4 725
清酒与你
清酒与你 2021-01-03 23:16

This is Ruby 1.8.7 but should be same as for 1.9.x

I am trying to split a string for example:

a = \"foo.bar.size.split(\'.\').last\"         


        
4条回答
  •  星月不相逢
    2021-01-03 23:33

    a = "foo.bar.size.split('.').last"
    p a.split(/(? ["foo", "bar", "size", "split('.')", "last"]
    

    You are looking for Lookahead and Lookbehind assertions. http://www.regular-expressions.info/lookaround.html

提交回复
热议问题