Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式验证CSV中的项目_Php_Javascript_Regex_Csv - Fatal编程技术网

Php 使用正则表达式验证CSV中的项目

Php 使用正则表达式验证CSV中的项目,php,javascript,regex,csv,Php,Javascript,Regex,Csv,我有一个CSV字符串,我正试图通过正则表达式验证它,以确保它只有N个项目。我尝试了以下模式(查找2项): 但我猜这似乎不起作用,因为内在模式不够贪婪 有什么想法吗?理想情况下,它应该与PHP和Javscript正则表达式引擎一起工作 更新: re_valid = r""" # Validate a CSV string having single, double or un-quoted values. ^ # Anchor t

我有一个CSV字符串,我正试图通过正则表达式验证它,以确保它只有N个项目。我尝试了以下模式(查找2项):

但我猜这似乎不起作用,因为内在模式不够贪婪

有什么想法吗?理想情况下,它应该与PHP和Javscript正则表达式引擎一起工作

更新:

re_valid = r"""
# Validate a CSV string having single, double or un-quoted values.
^                                   # Anchor to start of string.
\s*                                 # Allow whitespace before value.
(?:                                 # Group for value alternatives.
  '[^'\\]*(?:\\[\S\s][^'\\]*)*'     # Either Single quoted string,
| "[^"\\]*(?:\\[\S\s][^"\\]*)*"     # or Double quoted string,
| [^,'"\s\\]*(?:\s+[^,'"\s\\]+)*    # or Non-comma, non-quote stuff.
)                                   # End group of value alternatives.
\s*                                 # Allow whitespace after value.
(?:                                 # Zero or more additional values
  ,                                 # Values separated by a comma.
  \s*                               # Allow whitespace before value.
  (?:                               # Group for value alternatives.
    '[^'\\]*(?:\\[\S\s][^'\\]*)*'   # Either Single quoted string,
  | "[^"\\]*(?:\\[\S\s][^"\\]*)*"   # or Double quoted string,
  | [^,'"\s\\]*(?:\s+[^,'"\s\\]+)*  # or Non-comma, non-quote stuff.
  )                                 # End group of value alternatives.
  \s*                               # Allow whitespace after value.
)*                                  # Zero or more additional values
$                                   # Anchor to end of string.
"""
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
出于技术原因,我真的希望通过regex实现这一点,而不是另一个解决方案。CSV不带引号,且值不包含逗号,因此这不是问题

/([^,]*[,]{1}[^,]*){1}/
这就是我现在所处的位置,虽然很有效,但仍然有点难看,并且有一个匹配项的问题

CSV看起来像:

apples,bananas,pears,oranges,grapefruit

在PHP中,您最好使用以下函数:

它将处理以下问题:

a,"b,c"
。。。其中包含两项而不是三项


我不知道javascript的等效函数。

未经测试,因为我不知道您的输入是什么样的:

/^([^,]+,){1}([^,]+$)/
这需要两个字段(一个逗号,因此最后一个字段后面没有逗号)。

var vals=“something,sselse,anotherone,woohoo.”拆分(“,”),
最大长度=4;

返回vals.length根据CSV的格式,它可能能够在
/\”、\“/
(即双引号-逗号-双引号)上拆分,并获取结果数组的长度


正则表达式不太适合解析,因此如果字符串很复杂,您可能需要以其他方式对其进行解析。

使用
g
global
)修饰符使RegExp更简洁如何

var foobar = 'foo,bar',
    foobarbar = 'foo,bar,"bar"',
    foo = 'foo,',
    bar = 'bar';
foo.match(/([^,]+)/g).length === 2; //=> false
bar.match(/([^,]+)/g).length === 2; //=> false
foobar.match(/([^,]+)/g).length === 2; //=> true
foobarbar.match(/([^,]+)/g).length === 2; //=> false
明白了

/^([^,]+([,]{1}|$)){1}$/
将最后一个{N}设置为要检查的结果数量或范围{1,3}。

查看

引用:

re_valid = r"""
# Validate a CSV string having single, double or un-quoted values.
^                                   # Anchor to start of string.
\s*                                 # Allow whitespace before value.
(?:                                 # Group for value alternatives.
  '[^'\\]*(?:\\[\S\s][^'\\]*)*'     # Either Single quoted string,
| "[^"\\]*(?:\\[\S\s][^"\\]*)*"     # or Double quoted string,
| [^,'"\s\\]*(?:\s+[^,'"\s\\]+)*    # or Non-comma, non-quote stuff.
)                                   # End group of value alternatives.
\s*                                 # Allow whitespace after value.
(?:                                 # Zero or more additional values
  ,                                 # Values separated by a comma.
  \s*                               # Allow whitespace before value.
  (?:                               # Group for value alternatives.
    '[^'\\]*(?:\\[\S\s][^'\\]*)*'   # Either Single quoted string,
  | "[^"\\]*(?:\\[\S\s][^"\\]*)*"   # or Double quoted string,
  | [^,'"\s\\]*(?:\s+[^,'"\s\\]+)*  # or Non-comma, non-quote stuff.
  )                                 # End group of value alternatives.
  \s*                               # Allow whitespace after value.
)*                                  # Zero or more additional values
$                                   # Anchor to end of string.
"""
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
或可用表单(因为JS无法处理多行正则表达式字符串):

re_valid = r"""
# Validate a CSV string having single, double or un-quoted values.
^                                   # Anchor to start of string.
\s*                                 # Allow whitespace before value.
(?:                                 # Group for value alternatives.
  '[^'\\]*(?:\\[\S\s][^'\\]*)*'     # Either Single quoted string,
| "[^"\\]*(?:\\[\S\s][^"\\]*)*"     # or Double quoted string,
| [^,'"\s\\]*(?:\s+[^,'"\s\\]+)*    # or Non-comma, non-quote stuff.
)                                   # End group of value alternatives.
\s*                                 # Allow whitespace after value.
(?:                                 # Zero or more additional values
  ,                                 # Values separated by a comma.
  \s*                               # Allow whitespace before value.
  (?:                               # Group for value alternatives.
    '[^'\\]*(?:\\[\S\s][^'\\]*)*'   # Either Single quoted string,
  | "[^"\\]*(?:\\[\S\s][^"\\]*)*"   # or Double quoted string,
  | [^,'"\s\\]*(?:\s+[^,'"\s\\]+)*  # or Non-comma, non-quote stuff.
  )                                 # End group of value alternatives.
  \s*                               # Allow whitespace after value.
)*                                  # Zero or more additional values
$                                   # Anchor to end of string.
"""
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
可以使用RegEx.test()调用它。

第一个匹配项查找有效的单引号字符串。第二个匹配查找有效的双引号字符串,第三个匹配查找未引号字符串

如果删除单引号匹配项,则它几乎100%实现了可工作的spec CSV验证程序

注意:它可能是100%,但我不记得它是否可以处理值中的换行符(我认为[\S\S]是一个特定于javascript的检查换行符的黑客)

注意:这是一个只使用JavaScript的实现,不能保证正则表达式源字符串在PHP中工作


如果您计划对CSV数据进行任何非琐碎的处理,我建议您采用现有的库。如果您正在寻找一个符合RFC的实现,它会变得非常难看。

如果数据包含包含逗号的引用文本,它将不起作用。这是真的,我没有想到。
{1}
只是噪声,可以安全地忽略。是的,我将它放在那里,因为它显示了如果您希望它匹配更多的项,则要重复什么。这将失败。如果这也应该匹配(因为它是有效的csv),他们应该选择您的答案。:)