FasterCSV col separator


#1

Hi all,

I’m using FasterCSV and I can’t seem to get the syntax correct for using
‘|’ (the pipe character) for the column separator

I have

FasterCSV.parse(csv_file, { :col_sep => “|” }) do |row|
p row
end

but this fails with

invalid regular expression; there’s no previous pattern, to which ‘+’
would define cardinality at 4: /\A|+/

is this a known bug?

Kev


#2

Kev J. wrote:

but this fails with

invalid regular expression; there’s no previous pattern, to which ‘+’
would define cardinality at 4: /\A|+/

is this a known bug?

Kev

Probably, the col_sep value is simply being interpolated into a regex,
instead of first being treated with Regexp.escape. You can do the
escaping yourself:

irb(main):001:0> /\A|+/ =~ “foo”
SyntaxError: compile error
(irb):1: invalid regular expression; there’s no previous pattern, to
which ‘+’ would define cardinality at 4: /\A|+/
from (irb):1
irb(main):002:0> /\A|+/ =~ “foo”
=> nil
irb(main):003:0> /\A|+/ =~ “|foo”
=> 0
irb(main):004:0> s = “|”
=> “|”
irb(main):005:0> Regexp.escape s
=> “\|”

I don’t know anything about FasterCSV, but maybe the author should
consider escaping a string value for :col_sep before interpolating it in
a regexp.


#3

On Dec 2, 2005, at 2:34 AM, Kev J. wrote:

but this fails with

invalid regular expression; there’s no previous pattern, to which
‘+’ would define cardinality at 4: /\A|+/

is this a known bug?

It’s now known and fixed. Update to the freshly uploaded FasterCSV
0.1.4 and this should be just a bad memory. I’ve added test cases to
monitor this, so it shouldn’t happen again. Sorry about that.

James Edward G. II