Regex for Parsing Custom CSV Format in Ruby - implementing Escaped Quotes
I'm dealing with I've been researching this but I'm reviewing some code and Does anyone know how to Quick question that's been bugging me - I've searched everywhere and can't find a clear answer. I'm working on a Ruby application where I need to parse a custom CSV format. The format is similar to standard CSV, but values can include escaped quotes. For example, the input looks like this: ``` "value1, with comma","value2 \"escaped quote\"", "another value","yet another value" ``` I want to extract the values correctly while accounting for the escaped quotes. I've tried using the following regex: ``` /(?<=^|,)("(?:[^"\\]*(?:\\.[^"\\]*)*)"|[^,]*)/ ``` However, this regex is failing to handle the escaped quotes properly. When I run the code, I get unexpected results like `"escaped quote"` being split incorrectly into separate values. I've also attempted to debug it by breaking down the regex, but it seems overly complex and still fails on edge cases. Here's a snippet of my parsing function: ```ruby def parse_custom_csv(input) regex = /(?<=^|,)("(?:[^"\\]*(?:\\.[^"\\]*)*)"|[^,]*)/ matches = input.scan(regex) matches.flatten end input = <<~CSV "value1, with comma","value2 \"escaped quote\"" "another value","yet another value" CSV puts parse_custom_csv(input) ``` I'm using Ruby 3.0 and getting results that don't match my expectations. Specifically, the output does not include the escaped quotes as single values, and I need to figure out how to adjust my regex to handle this properly. Any suggestions on how to refine my regex or parse logic to correctly interpret the values? For context: I'm using Ruby on Ubuntu. Any pointers in the right direction? I'm coming from a different tech stack and learning Ruby. Is this even possible? I'm working in a Windows 10 environment.