csv 文件中引号内字符串的正则表达式
Regex for String within quotes in csv file
我花了一上午的时间寻找答案,但找不到。
我需要使用正则表达式从 .csv 中提取字段。问题是,一个字段是注释字符串,因此它包括逗号、引号和许多其他字符。
这是我来自 .csv 的示例文本:
> "field1","field2","field3","A description like this, with "quotes" inside quotes and also, commas. eeehhh#### ","field4","field5","field6"
这是我的正则表达式(我尝试使用许多其他选项,例如 \B","\B 和其他选项,但都不起作用。
^\"(?<field1>[^\"]*)\"\,\"(?<field2>[^\"]*)\"\,\"(?<field3>[^\"]*)\"\,\"(?<description>[^\"]*)\"\,\"(?<field4>[^\"]*)\"\,\"(?<field5>[^\"]*)\"\,\"(?<field6>[^\"]*)\"
有没有办法告诉捕获组 读取任何内容,直到它完全找到字符串 ",""是我的分隔符?
我不知道我还能尝试什么。
有人可以帮我一下吗?
谢谢! :)
对于我的方法,使用@jaytea 提供的解决方案非常有效,就是这个:
Have you tried using a non-greedy ".?" ie. \"(?.?)\"\,\"?
The additional "?" tells the regex engine to match as few characters
as possible. Is that supported by your regex flavour?
希望这对以后遇到和我一样麻烦的人有所帮助。谢谢!
我花了一上午的时间寻找答案,但找不到。 我需要使用正则表达式从 .csv 中提取字段。问题是,一个字段是注释字符串,因此它包括逗号、引号和许多其他字符。 这是我来自 .csv 的示例文本:
> "field1","field2","field3","A description like this, with "quotes" inside quotes and also, commas. eeehhh#### ","field4","field5","field6"
这是我的正则表达式(我尝试使用许多其他选项,例如 \B","\B 和其他选项,但都不起作用。
^\"(?<field1>[^\"]*)\"\,\"(?<field2>[^\"]*)\"\,\"(?<field3>[^\"]*)\"\,\"(?<description>[^\"]*)\"\,\"(?<field4>[^\"]*)\"\,\"(?<field5>[^\"]*)\"\,\"(?<field6>[^\"]*)\"
有没有办法告诉捕获组
有人可以帮我一下吗?
谢谢! :)
对于我的方法,使用@jaytea 提供的解决方案非常有效,就是这个:
Have you tried using a non-greedy ".?" ie. \"(?.?)\"\,\"? The additional "?" tells the regex engine to match as few characters as possible. Is that supported by your regex flavour?
希望这对以后遇到和我一样麻烦的人有所帮助。谢谢!