On 03/10/05, Alex Fenton <alex / deleteme.pressure.to> wrote: > 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"] > > You can avoid toothpick-ities by using a custom regexp delimiter. E.g. irb(main):001:0> "this/is/a/path".split(%r{/}) => ["this", "is", "a", "path"] instead of irb(main):002:0> "this/is/a/path".split(/\//) => ["this", "is", "a", "path"] You can even do irb(main):003:0> "this/is/a/path".split(%r / ) => ["this", "is", "a", "path"] or even if there are no spaces in the regexp irb(main):004:0> "this/is/a/path".split %r / => ["this", "is", "a", "path"] (but that is dangerous because you need a trailing space and was done more for fun) regards, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/