Rya*_*ard 5 php regex preg-match-all
我正在使用PHP preg_match_all,这是我到目前为止所能获得的....
[A-Za-z+\W]+\s[\d]
Run Code Online (Sandbox Code Playgroud)
唯一的问题是我需要\ W不是一个".
所以我试过了:
[A-Za-z+[^\dA-Za-z"]\s?]+\s[\d]
[A-Za-z+]\s?+[^A-Za-z\d"]?\s[\d]
Run Code Online (Sandbox Code Playgroud)
除其他外,它只是失败,我真的无法弄清楚为什么.
这是整个RegEx;
([A-Z][a-z]+\s){1,5}\s?[^a-zA-Z\d\s:,.\'\"]\s?
[A-Za-z+\W]+\s[\d]{1,2}\s[A-Z][a-z]+\s[\d]{4}
Run Code Online (Sandbox Code Playgroud)
我把它分成两行,第二行从我发布的内容开始.
试图匹配的模式:
India – Adulterated Tea Powder Seized 18 April 2011
India – Importer of Haldiram’s Petha Sweet Cubes Issuing Voluntary Recall 26 April 2011
India – Undeclared Gluten Found in Sweets by Canadian Authorities 27 April 2011
India – Adulteration Found in Edible Oils 28 April 2011
India – Viral Disease Affects Chili Crop in Goa 28 April 2011
NOT ----> Chili – India: Goa”. 8 April 2011
Ivory Coast – Potential Cocoa Quality Decline despite Sufficient Surplus 11 April 2011
Japan – Sanuki Kanzume Co. and Failure to Comply with FDA Standards 27 April 2011
Madagascar – Toxic Sardines 14 April 2011
Madagascar – Update: Toxic Sardines 26 April 2011
Run Code Online (Sandbox Code Playgroud)
您显示的模式匹配所有字母和非字符.唯一没有包含在模式中的是数字,你也想要与双引号不匹配.
[^\d\"_]+\s\d
Run Code Online (Sandbox Code Playgroud)
编辑:
我可能是错的,但是从示例输入来看,您似乎只是想匹配所有没有双引号的行.如果这样的事情更容易,我甚至将日期与字符串的其余部分分开.如果您不需要对sting/date进行分组,则只需删除所有括号.
<?php
error_reporting(E_ALL);
$str = " India - Adulterated Tea Powder Seized 18 April 2011
India - Importer of Haldiram’s Petha Sweet Cubes Issuing Voluntary Recall 26 April 2011
India - Undeclared Gluten Found in Sweets by Canadian Authorities 27 April 2011
India - Adulteration Found in Edible Oils 28 April 2011
India - Viral Disease Affects Chili Crop in Goa 28 April 2011
Chili - India: Goa\". 8 April 2011
Ivory Coast - Potential Cocoa Quality Decline despite Sufficient Surplus 11 April 2011
Japan - Sanuki Kanzume Co. and Failure to Comply with FDA Standards 27 April 2011
Madagascar - Toxic Sardines 14 April 2011
Madagascar - Update: Toxic Sardines 26 April 2011";
preg_match_all("/^([^\"]+?)(\d?\d\s[a-z]+\s\d{4})$/im", $str, $m);
echo '<pre>'.print_r($m, true).'</pre>';
?>
Run Code Online (Sandbox Code Playgroud)