Regular Expressions - how to replace a character within quotes

前端 未结 3 1684
执念已碎
执念已碎 2020-12-16 03:16

Hello regular expression experts,

There has never been a string manipulation problem I couldn\'t resolve with regular expressions until now, at least in an elegant m

3条回答
  •  青春惊慌失措
    2020-12-16 03:30

    I'll help you, but you have to promise to stop using the word "elegant". It's been working too hard lately, and deserves a rest. :P

    (?m),(?=[^"]*"(?:[^"\r\n]*"[^"]*")*[^"\r\n]*$)
    

    This matches a comma if, between the comma and the end of the record, there's an odd number of quotation marks. I'm assuming a standard CSV format, in which a record ends at the next line separator that isn't enclosed in quotes. Line separators are legal inside quoted fields, as are quotes if they're escaped with another quote.

    Depending on which regex flavor you're using, you may have to use \r?$ instead of just $. In .NET, for example, only the linefeed (\n) is considered a line separator. But in Java, $ matches before the \r in \r\n, but not between the \r and the \n (unless you set UNIX_LINES mode).

提交回复
热议问题