【问题标题】:regex: split a long string with tabulations and newlines正则表达式:用制表符和换行符分割一个长字符串
【发布时间】:2017-12-16 06:12:10
【问题描述】:

考虑以下字符串:

08/07/2017Peter Praet: Interview with De StandaardInterview with Peter Praet,  Member of the Executive Board of the ECB,  conducted by Pascal Dendooven and Goele De Cort on 3 July 2017,  published on 8 July 2017ENGLISH\n\t\t\t\t\t\t\tOTHER LANGUAGES\n\t\t\t\t\t\t\t(1)\n\t\t\t\t\t\t\t+\n\t\t\t\t\t\t\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tSelect your language\n\t\t\t\t\t\t\t\n\t\t\t\t\t\tNederlandsNL07/07/2017Benoît Cœuré: Interview with Le Monde and La StampaInterview with Benoît Cœuré,  Member of the Executive Board of the ECB,  conducted by Marie Charrel (Le Monde) and Alessandro Barbera (La Stampa),  on 3 July,  published on 7 July 2017ENGLISH"

我想把里面的两句话提取出来,分别是:

  • "08/07/2017Peter Praet: Interview with De StandaardInterview with Peter Praet, Member of the Executive Board of the ECB, conducted by Pascal Dendooven and Goele De Cort on 3 July 2017, published on 8 July 2017ENGLISH"

  • "NederlandsNL07/07/2017Benoît Cœuré: Interview with Le Monde and La StampaInterview with Benoît Cœuré, Member of the Executive Board of the ECB, conducted by Marie Charrel (Le Monde) and Alessandro Barbera (La Stampa), on 3 July, published on 7 July 2017ENGLISH"

我尝试使用[\w]+(?!\\t),但这会捕获t(1 中的t 以及其他内容。

这里的正确语法是什么? 谢谢!

【问题讨论】:

  • 您使用哪种编程语言?因为他们的regexpr 每个都略有不同。另外,你是否试图从这个字符串中提取东西?或者正则表达式也必须适用于其他类似事件?
  • 尝试\w{4,} 而不是[\w]+。这将消除三个或更少字符的匹配,而且我很确定 \w 已经是一个字符类:不需要括号。
  • 在 Python 中,您可以在制表符和换行符上执行 re.split 并过滤掉少于 20 个字符的任何内容。
  • 这就像去射击场磨练你的剑术 :) 找到一个真正受益于纯正则表达式解决方案的问题并磨练出来。
  • 给你,分开这个r'(?:\\[\\ntr])+(?:(?:(?!\\[\\ntr]).)*\\[\\ntr])*'regex101.com/r/lNv8VO/1

标签: python r regex


【解决方案1】:

给你,分开吧

r'(?:\\[\\ntr])+(?:(?:(?!\\[\\ntr]).)*\\[\\ntr])*'

http://www.regex101.com/r/lNv8VO/1

解释

 (?: \\ [\\ntr] )+             # The start of a block of escaped \ or n or t or r
                               # Get as many as are there (like '\n\n\r\r\t\t\n\\', etc)
 (?:                           # Cluster optional
      (?:                           # ----------
           (?! \\ [\\ntr] )              # Not an escaped \ or n or t or r ahead
           .                             # This is ok, consume this
      )*                            # ---------- 0 to many times
      \\ [\\ntr]                    # A required escaped \ or n or t or r at the end
 )*                            # Cluster end, do 0 to many times

注意
上面的正则表达式会将 most 处的文本分成 2 个部分。

如果拆分内容包含非转义的 r,n,t,那么您可以允许 for
如果文本低于某个阈值,则进行多次拆分。

@MadPhysicist 建议长度为 20。我会给它 40,并在
正则表达式,在本节(?:(?:(?!\\[\\ntr]).){0,20} 中给它一个范围。

新的正则表达式是

r'(?s)(?:\\[\\ntr])+(?:\s*(?:(?!\\[\\ntr]).){0,40}?\s*\\[\\ntr])*'

https://regex101.com/r/lNv8VO/3

解释

 (?s)                          # Modifiers:  dot-all
 (?: \\ [\\ntr] )+             # The start of a block of escaped \ or n or t or r
                               # Get as many as are there (like '\n\n\r\r\t\t\n\\', etc)
 (?:                           # Cluster optional
      \s*                           # Optional whitespace
      (?:                           # ----------
           (?! \\ [\\ntr] )              # Not an escaped \ or n or t or r ahead
           .                             # This is ok, consume this
      ){0,40}?                      # ---------- Allow (non-greedy) 0 to 40 characters for multiple sections
      \s*                           # Optional whitespace
      \\ [\\ntr]                    # A required escaped \ or n or t or r at the end
 )*                            # Cluster end, do 0 to many times

【讨论】:

  • 大吃一惊。为什么你有一个三重 (?:(?:(?! ?这确实是正则表达式地狱:D
  • @Noobie - 相当于语言中的代码块 scpoing。如果您查看 formatted 版本,则很容易破译。我已经为你添加了更新说明..
【解决方案2】:

在 Python 中,您可以根据制表符和换行符拆分字符串,然后过滤掉太短的内容。

import re

[x for x in re.split('\n\t+', long_string) if len(x) > 20]

【讨论】:

    【解决方案3】:

    假设 \n 和 \t 字符实际上是换行符和制表符。试试:

    ([^\n\t]*)
    

    然后增加它以摆脱其他语言等。

    【讨论】:

    • 我认为它们不是换行符和制表符,只是看起来像那样的文字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多