我正在尋找一個在PHP 5中使用preg_match_all的正則表達式,它允許我用逗號分割一個字符串,只要逗號不存在在單引號內,允許使用單引號。示例數據將是:REGEX:通過不用單引號的逗號分割,允許使用轉義引號
(some_array, 'some, string goes here','another_string','this string may contain "double quotes" but, it can\'t split, on escaped single quotes', anonquotedstring, 83448545, 1210597346 + '000', 1241722133 + '000')
這應該產生匹配,看起來像這樣:
(some_array
'some, string goes here'
'another_string'
'this string may contain "double quotes" but, it can\'t split, on escaped single quotes'
anonquotedstring
83448545
1210597346 + '000'
1241722133 + '000')
我試過很多,很多的正則表達式...我現在的一個看起來是這樣的,雖然它不不能正確匹配100%。 (它仍然在單引號內部分開一些逗號。)
"/'(.*?)(?<!(?<!\\\)\\\)'|[^,]+/"
此*能*來完成,但是它是相當困難比大多數人想象的要多看起來你現在正在感受到困難。它真的沒有庫函數在PHP中處理這個問題嗎?有在Perl中。如果到那時你還沒有得到一個好的答案,我可能會試着把這個正則表達式放在一起給你。 – tchrist 2010-11-22 14:11:50