From: Alex Fenton Date: 2005-10-03T22:31:47+09:00 Subject: Re: split without lookbehind Thanks all for your suggestions. I had forgotten scan was handy in situations like this - I ended up going with a solution similar to Gavin's (again using commas to avoid toothpick-itis) str.scan /(?:[^,]|,,)+/ Also helped me see that there are some tricky edge-cases where the separator character is at the beginning or end of an element - I'm going to prohibit this in the appplication. cheers alex Gavin Kistner wrote: > 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"]