到目前为止我发现的是
preg_match_all("/'[^']+'|[^,]+/", $input, $output);
它应该在简单的引号之外找到逗号,我需要将其改编为双引号。
答案 0 :(得分:2)
不确定单引号的原始模式是否正确,这个会在双引号外找到所有逗号:
preg_match_all('~"(?:[^\\\"]+(?:\\\.)*|\\\.)*+"(*SKIP)(*F)|,~s', $subject, $matches);
模式细节:
~
"
(?: # all possible content between quotes
[^\\\"]+ # all that is not a double quote or a backslash
(?:\\\.)* # eventual escaped characters
| # OR
\\\. # an escaped character
)*+ # repeat zero or more times (possessive)
" # closing double quote, can be replaced with (?:"|\z) or "?
(*SKIP)(*F) # forces the pattern to fail and to not retry double quoted parts
| # OR
, # a comma
~
s # allow the dot to match newlines characters
注意:如果您想将孤立双引号后的子字符串视为带引号的子字符串(直到字符串的结尾),您可以使用(?:"|\z)
或更多简单{替换模式中的结束双引号{1}}
注意2:为了大幅减少找到匹配所需的步骤数,可以像这样重写模式:
"?
或者如果你想使用第一个字符识别技术:
~[^,"]*+\K(?:"[^"\\\]*+(?:(?:\\\.)+[^\\\"]*)*+"?|,(*ACCEPT)|\z(*COMMIT).)(*SKIP)(*F)~s
答案 1 :(得分:1)