正则表达式匹配分裂
问题描述:
fontSize=16.0, fontFamily=sans, align=0, color=FF0000, text="foo, bar"
和我需要匹配为吐。输出将被正则表达式匹配分裂
array(
'fontSize'=>'16.0',
'fontFamily'=>'sans',
'align'=>'0',
'color'=>'FF0000',
'text'=>'foo, bar'
);
我想未来,但它是坏的:
preg_spit("~[\s]="?[\s]"?,~", $string);
答
根据下面的正则表达式只是分割你输入的字符串,
,\s(?![^=]*")
<?php
$str = 'fontSize=16.0, fontFamily=sans, align=0, color=FF0000, text="foo, bar"';
$regex = '~,\s(?![^=]*")~';
$splits = preg_split($regex, $str);
print_r($splits);
?>
输出:
Array
(
[0] => fontSize=16.0
[1] => fontFamily=sans
[2] => align=0
[3] => color=FF0000
[4] => text="foo, bar"
)
正则表达式:
, ','
\s whitespace (\n, \r, \t, \f, and " ")
(?! look ahead to see if there is not:
[^=]* any character except: '=' (0 or more
times)
" '"'
) end of look-ahead
+0
这就是它!问候 – 2014-11-21 08:44:49
'preg_spit( “〜[\ S] = \”[\ S] \ “?〜”,$弦);' – 2014-11-21 08:37:53
你不能用'分裂,',因为,在''foo,bar“' – 2014-11-21 08:38:49