匹配 CSV 分隔符的正则表达式 [英] Regular expression to match CSV delimiters

查看:67
本文介绍了匹配 CSV 分隔符的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 PCRE,它仅匹配 CSV 文件中用作分隔符的逗号.假设一行的格式是这样的:

I'm trying to create a PCRE that will match only the commas used as delimiters in a line from a CSV file. Assuming the format of a line is this:

1,"abcd",2,"de,fg",3,"hijk"

我想匹配除e"和f"之间的逗号之外的所有逗号.或者,只匹配那个是可以接受的,如果这是更简单或更明智的解决方案.我有一种感觉,我需要使用否定的前瞻断言来处理这个问题,但我发现它有点难以弄清楚.

I want to match all of the commas except for the one between the 'e' and 'f'. Alternatively, matching just that one is acceptable, if that is the easier or more sensible solution. I have the sense that I need to use a negative lookahead assertion to handle this, but I'm finding it a bit too difficult to figure out.

推荐答案

见我的 解决此问题的帖子 了解更多详情.

See my post that solves this problem for more detail.

^(?:(?:"((?:""|[^"])+)"|([^,]*))(?:$|,))+$ 将匹配整行,然后你可以使用 match.Groups[1 ].Captures 来获取你的数据(不带引号).另外,我让 "My name is ""in引号""" 是一个有效的字符串.

^(?:(?:"((?:""|[^"])+)"|([^,]*))(?:$|,))+$ Will match the whole line, then you can use match.Groups[1 ].Captures to get your data out (without the quotes). Also, I let "My name is ""in quotes""" be a valid string.

这篇关于匹配 CSV 分隔符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆