Php 删除新闻文件中的重复数据
我们有以下格式的新闻发布数据。Php 删除新闻文件中的重复数据,php,regex,Php,Regex,我们有以下格式的新闻发布数据。\t是此处的实际制表符 Headline\tDate\tNews 问题是在过去,类似这样的重复或额外字段存在一些问题 Government Shutdown Latest News {null}{10/15/2013} {10/15/2013} words words words. Email Flow in Exchange {null}{10/17/2013} {10/17/2013} words words words.... Should
\t
是此处的实际制表符
Headline\tDate\tNews
问题是在过去,类似这样的重复或额外字段存在一些问题
Government Shutdown Latest News {null}{10/15/2013} {10/15/2013} words words words.
Email Flow in Exchange {null}{10/17/2013} {10/17/2013} words words words....
Should This be banned? {null}{10/23/2013} {10/23/2013} words words words....
我需要删除1st括号字段{null}
和3rd重复字段,以及第3个字段后面的制表符
所以这些数据的每一行最初应该是这样的
Government Shutdown Latest News {10/15/2013} words words words....
Email Flow in Exchange {10/17/2013} {10/17/2013} words words words....
Should This be banned? {10/23/2013} {10/23/2013} words words words....
但是,我无法仅删除这两个字段和选项卡。它们都匹配
preg_replace('/\{.*?\}(?={)|\{.*?\}\t/', '', $text);
您可以对作业使用负面后顾法
(?<![^\s]){[^}]*}\t?
(?
正则表达式:
(?<! look behind to see if there is not:
[^\s] any character except: whitespace (\n, \r, \t, \f, and " ")
) end of look-behind
{ '{'
[^}]* any character except: '}' (0 or more times)
} '}'
\t? '\t' (tab) (optional)
(?
注意:您可以在不转义{}
的情况下执行此操作
有关此模式和的信息,请参见。您可以尝试此模式:
$result = preg_replace('~[^\s}]\s*\K{null}|{[0-9]{2}/[0-9]{2}/[0-9]{4}}\t(?!\s*[^{])~', '', $text);
我遇到的唯一问题是,如果null字段有不同的值,我可以更改它以匹配任何值吗?@paulie.jvenuez:是的,您可以用[^}]*
替换null
。谢谢您的帮助。回答得好,这在大多数情况下都有效。但有时不匹配其他换行符?