Forum: Ruby FasterCSV col separator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
kevin.jackson (Guest)
on 2005-12-02 10:36
(Received via mailing list)
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
vjoel (Guest)
on 2005-12-02 10:48
(Received via mailing list)
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.
James G. (Guest)
on 2005-12-02 17:11
(Received via mailing list)
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
This topic is locked and can not be replied to.