描述
如果您想简单地解析每个逗号分隔的字段,这些字段可能被双引号包围,也可能不被双引号包围,您可以使用这个正则表达式:
(?:^|,)("?)(.*?)\1(?=,(?!\s)|$)
第 2 组被分配了每个逗号分隔的值。如果该值由引号打开,则需要一个右引号后跟,
一个空格,或者需要行尾来关闭字符串。
PHP 代码示例:
<?php
$sourcestring="your source string";
preg_match_all('/(?:^|,)("?)(.*?)\1(?=,|$)/ims',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => 200000
[1] => ,Samsung Galaxy S2
[2] => ,$399.00
[3] => ,8806085359376
[4] => ,null
[5] => ,Free ground shipping
[6] => ,New
[7] => ,In Stock
[8] => ,Samsung
[9] => ,"Vivid‧Fast‧Slim The new GALAXY SII Plus makes your life even smarter! 4.3" SUPER AMOLED Plus The 4.3" SUPER AMOLED Plus display goes a step beyond the already remarkable SUPER AMOLED to provide enhanced readability, a slimmer design, and better battery consumption for the best viewing value of any smartphone. Full-Touch Display Size: 4.3" Resolution: 480 x 800pixel Platform Operation Platform: Android v4.1 (Jelly Bean) TOUCHWiZ v4.0 User Interface (upto 7 pages widget desktop) Band^ UMTS(850 / 900 / 1900 / 2100MHz)+ Battery Capacity: 1650mAh"
[10] => ,Mobile > Manufacturer > Samsung
[11] => ,
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] => "
[10] =>
[11] =>
)
[2] => Array
(
[0] => 200000
[1] => Samsung Galaxy S2
[2] => $399.00
[3] => 8806085359376
[4] => null
[5] => Free ground shipping
[6] => New
[7] => In Stock
[8] => Samsung
[9] => Vivid‧Fast‧Slim The new GALAXY SII Plus makes your life even smarter! 4.3" SUPER AMOLED Plus The 4.3" SUPER AMOLED Plus display goes a step beyond the already remarkable SUPER AMOLED to provide enhanced readability, a slimmer design, and better battery consumption for the best viewing value of any smartphone. Full-Touch Display Size: 4.3" Resolution: 480 x 800pixel Platform Operation Platform: Android v4.1 (Jelly Bean) TOUCHWiZ v4.0 User Interface (upto 7 pages widget desktop) Band^ UMTS(850 / 900 / 1900 / 2100MHz)+ Battery Capacity: 1650mAh
[10] => Mobile > Manufacturer > Samsung
[11] =>
)
)
简单替换
因为您的源文本是逗号分隔的,并且逗号分隔符没有任何周围的空间来解决"excellent occasion, 4.3", samsung"
您可以使用的问题
正则表达式:(?<!,)(")(?!,\S)
替换为空
PHP 代码示例:
<?php
$sourcestring="your source string";
echo preg_replace('/(?<!,)(")(?!,\S)/ims','',$sourcestring);
?>
$sourcestring after replacement:
200000,Samsung Galaxy S2,$399.00,8806085359376,null,Free ground shipping,New,In Stock,Samsung,"Vivid‧Fast‧Slim The new GALAXY SII Plus makes your life even smarter! 4.3 SUPER AMOLED Plus The 4.3 SUPER AMOLED Plus display goes a step beyond the already remarkable SUPER AMOLED to provide enhanced readability, a slimmer design, and better battery consumption for the best viewing value of any smartphone. Full-Touch Display Size: 4.3 Resolution: 480 x 800pixel Platform Operation Platform: Android v4.1 (Jelly Bean) TOUCHWiZ v4.0 User Interface (upto 7 pages widget desktop) Band^ UMTS(850 / 900 / 1900 / 2100MHz)+ Battery Capacity: 1650mAh",Mobile > Manufacturer > Samsung,