From: Gavin Kistner Date: 2005-10-03T22:01:07+09:00 Subject: Re: split without lookbehind --Apple-Mail-1--286590937 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On Oct 2, 2005, at 1:06 PM, Alex Fenton wrote: > I'm trying to split a path (like an URL, but not one) into bits on > the '/' character. > Problem is, I want to be able to be able to escape '/' in the names > of bits by doubling > the character to '//' eg > > 'foo/bar // baz/qux' > => ["foo", "bar / baz", "qux"] Lookbehind is what you want; until we get it in core ruby, I've provided an example of another way to get the above. I've used commas instead of / just to help make the regexp easier to understand - substitute with "\/" as appropriate: str = 'foo,bar ,, baz,qux,,,jorb,jing,,,,blat' out = [] str.scan( /(.+?[^,],{2}*)(?:,(?!,)|$)/ ){ |a,b| out << a.gsub( ',,', ',' ) } p out #=> ["foo", "bar , baz", "qux,", "jorb", "jing,,blat"] --Apple-Mail-1--286590937--