Skip to content

Commit 0dcfcd9

Browse files
docs: Add entry for backslash_quotes liberal parsing (#280)
1 parent cc6b47a commit 0dcfcd9

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
====== Option +liberal_parsing+
22

3-
Specifies the boolean value that determines whether
3+
Specifies the boolean or hash value that determines whether
44
CSV will attempt to parse input not conformant with RFC 4180,
55
such as double quotes in unquoted fields.
66

77
Default value:
88
CSV::DEFAULT_OPTIONS.fetch(:liberal_parsing) # => false
99

10-
For examples in this section:
10+
For the next two examples:
1111
str = 'is,this "three, or four",fields'
1212

1313
Without +liberal_parsing+:
@@ -17,3 +17,22 @@ Without +liberal_parsing+:
1717
With +liberal_parsing+:
1818
ary = CSV.parse_line(str, liberal_parsing: true)
1919
ary # => ["is", "this \"three", " or four\"", "fields"]
20+
21+
Use the +backslash_quote+ sub-option to parse values that use
22+
a backslash to escape a double-quote character. This
23+
causes the parser to treat <code>\"</code> as if it were
24+
<code>""</code>.
25+
26+
For the next two examples:
27+
str = 'Show,"Harry \"Handcuff\" Houdini, the one and only","Tampa Theater"'
28+
29+
With +liberal_parsing+, but without the +backslash_quote+ sub-option:
30+
# Incorrect interpretation of backslash; incorrectly interprets the quoted comma as a field separator.
31+
ary = CSV.parse_line(str, liberal_parsing: true)
32+
ary # => ["Show", "\"Harry \\\"Handcuff\\\" Houdini", " the one and only\"", "Tampa Theater"]
33+
puts ary[1] # => "Harry \"Handcuff\" Houdini
34+
35+
With +liberal_parsing+ and its +backslash_quote+ sub-option:
36+
ary = CSV.parse_line(str, liberal_parsing: { backslash_quote: true })
37+
ary # => ["Show", "Harry \"Handcuff\" Houdini, the one and only", "Tampa Theater"]
38+
puts ary[1] # => Harry "Handcuff" Houdini, the one and only

0 commit comments

Comments
 (0)