【问题标题】:RegEx to delete all double whitespace EXCEPT \n? preg_replace正则表达式删除所有双空格,除了 \n? preg_replace
【发布时间】:2011-08-30 00:47:48
【问题描述】:

我已经使用 Python 脚本导入了 PDF 的纯文本版本,但它有一堆我不关心的垃圾伪影。

我关心的唯一空格是 (1) single 个空格,和 (2) double \n's。

单个空格, 出于显而易见的原因,在单词边界之间。 双\n,用于区分段落。

它包含的 垃圾 空格如下所示:

[\ \n\t]+ all jumbled together

这导致我遇到另一个问题,有时段落由

划分
[\n][\s]+[\n]

我对正则表达式的经验不足,无法使其忽略两个\n 之间的内部空格。作为一个业余RegExer,我的问题是\s 包含\n

如果没有——我认为这将是一个非常容易解决的问题。

所有其他空白都是无关紧要的,我正在尝试的任何东西都没有真正起作用。

任何建议将不胜感激。

示例文本

Summary: The Department of Environment in Bangladesh seized 265 sacks of poultry feed 
tainted with tannery waste and various chemicals. 

Synthesis/Analysis: The Department of Environment seized the tainted poultry feed on 
28 March from a house in the city of Adabar located in Dhaka province. Workers were 
found in the house, which was used as an illegal factory, producing the tainted feed. The 
Bangladesh Environment Conservation Act allowed for a case to be filed against the 
factory’s manager, Mahmud Hossain, and the owner, who was not named. 

It was reported that the Department of Environment had also closed three other factories 
in Hazaribag a month prior to this instance for the same charges. The Bangladesh Council of 
Scientific and Industrial Research found that samples from the feed taken from these 
factories had “dangerous levels of chromium…”  The news report also stated that “poultry 
6 



and eggs became poisonous” from consuming the tainted feed, which would also cause 
health concerns for consumers. 

这只是引导我进行更多修复...必须删除所有页码和随机双 \n。

【问题讨论】:

  • 查看您提出的正则表达式问题的数量。如果您开始认真学习它们,您认为值得您(和我们的)时间吗?如果你仔细阅读 OReilly Regex 书,这并不难。
  • 感谢您的评论迈克。我很高兴 RegEx 对您来说很容易。
  • 迈克的评论是正确的。如果你必须问那么多问题,也许你应该研究这个主题,而不是继续要求人们为你做事。我自己喜欢正则表达式食谱。

标签: php html regex preg-replace


【解决方案1】:

您可以使用断言使\s 排除换行符:

 ((?!\n)\s){2,}

要将换行符与\n\s+\n 之间的空格合并,您可以使用类似的结构来代替\s+。但为简单起见,我只使用两个 preg_matches 并首先合并换行符,然后清理双空格。

【讨论】:

  • 我从未听说过这个断言,我现在正在谷歌上搜索它以获得更多解释!
  • 抱歉,忘记链接了:regular-expressions.info/lookaround.html 对前瞻和后置断言以及 ?!负变体。
  • 这就是我最后使用$find[5]='/[\s]*((?!\n)\s){2,}[\s]*/'; $fix[5]='<br/><br/>';
  • @mario 怎么能做同样的事情,但要保护<pre> 标签内的空白?
  • @Lea:事先将这些区域分开。
【解决方案2】:

我认为这会奏效:

  1. 将“\s*\n\s*\n\s*”替换为“\n\n”。

    这将使段落分隔符标准化。

  2. 将“\s* \s*”替换为“”。

    这将标准化单词分隔符。

【讨论】:

    【解决方案3】:
    s/(\h)+/$1/g;
    s/\n(\s*\n)+/\n\n/g;
    

    以上将删除水平空格(我假设您不担心垂直制表符或回车)和双空格上的所有换行符。

    【讨论】:

      【解决方案4】:

      有点老套,但我认为这应该可以解决问题:

      preg_replace('/\t|( ){2,}|(\n)\s+(\n)/', '\1\2\3', $data);

      奖励:不需要对字符串进行两次传递。

      【讨论】:

      • \1\2\3 只是替换字符串右侧部分的技巧。你有机会上传一些测试数据吗?如果不能正确尝试,很难做到正确
      • 当然...这是一个庞大的文件...所以我将发布一点点
      • 我认为 Stack Overflow 在我发布文件时也在清理文件。哈哈
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2011-11-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多