AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1714644
Accepted
DynV
DynV
Asked: 2022-04-06 04:42:59 +0800 CST2022-04-06 04:42:59 +0800 CST 2022-04-06 04:42:59 +0800 CST

使TSV脱离多行列表

  • 772

我有一个项目列表,其中每个项目都有多行。分隔项目的标记是唯一的(每个项目, HTML <li>),我只看到包含在单个标记化段落 (HTML <p>) 中的文本实例。我想用它制作一个 TSV,按顺序排列哪些物品:

  1. 日期
  2. 姓名
  3. 网址
  4. 概括

从我所看到的所有项目中,URL 和名称都有重复项(在每个项目中),所以我选择了第一个 URL 和第二个名称,因为这对我来说似乎最简单。摘要可能包含视觉辅助标签(即<strong>),所以我使用否定的前瞻来完成它,而不是不应该有内部标签的日期,所以我使用了否定的字符类。

前 2 项是

    <li><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/550756-america-long-war-race/">On
    Contact: Race and America's long war </a>
    </p>
    <p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/550756-america-long-war-race/">
  <font color="#000080">
    <img src="rt.com-on_contact-220405-no_blurb_html_1dff87941f1c724a.jpg" name="Image1" alt="On Contact: Race and America's long war" align="bottom" width="280" height="157" border="1"/>
  </font>
</a>
</p>
    <p style="margin-bottom: 0in">On the show, Chris Hedges discusses
    America's inner and outer wars and its nexus with capitalism and
    empire with Professor of Social and Cultural Analysis and History at
    New York University Nikhil Pal Singh. The internal violence in the
    United... 
    </p>
    <p style="margin-bottom: 0in">Feb 27, 2022 10:36</p>
    <li><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/550319-george-washington-genocidal-colonist/">
  <font color="#000080">
    <img src="rt.com-on_contact-220405-no_blurb_html_198feb67032166ff.png" name="Image3" alt="On Contact: George Washington and the legacy of white supremacy" align="bottom" width="280" height="157" border="1"/>
  </font>
</a>
</p>
    <p style="margin-bottom: 0in"><strong><a href="https://www.rt.com/shows/on-contact/550319-george-washington-genocidal-colonist/">On
    Contact: George Washington and the legacy of white supremacy </a></strong>
    </p>
    <p style="margin-bottom: 0in">On the show, Chris Hedges discusses
    George Washington, the fallible human being and one of the principal
    architects of the United States, with author Nathaniel Philbrick. As
    America fractures into ideologically hostile camps, it colors how
    we... 
    </p>
    <p style="margin-bottom: 0in">Feb 25, 2022 09:09 
    </p>
    <li>[...]

我尝试的正则表达式是<li>.*<a href="([^"]+)".*alt="On Contact: ([^"]+)".*<p[^>]*>((?:.(?!<\/p>))+)<\/p><p[^>]*>([^<]+)<,如果它有效,它将被替换为$4\t$2\t$1\t$3. 我希望正则表达式在 Notepad++ 中工作。

感谢您的帮助

更新 1

我后来使用的测试字符串添加了列表项,在摘要中添加了显示标签(即<strong>),虽然它与标题不一致,但我不得不删除标签,因为它们干扰 TSV 创建,我想我不妨删除其中的换行符过程(删除[\t\r\n]),导致:

<li><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/550756-america-long-war-race/">OnContact: Race and America's long war </a></p><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/550756-america-long-war-race/">  <font color="#000080">    <img src="rt.com-on_contact-220405-no_blurb_html_1dff87941f1c724a.jpg" name="Image1" alt="On Contact: Race and America's long war" align="bottom" width="280" height="157" border="1"/>  </font></a></p><p style="margin-bottom: 0in">On the show, Chris Hedges discussesAmerica's inner and outer wars and its nexus with capitalism and <strong>empire</strong> with Professor of Social and Cultural Analysis and History atNew York University Nikhil Pal Singh. The internal violence in theUnited... </p><p style="margin-bottom: 0in">Feb 27, 2022 10:36</p><li><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/550319-george-washington-genocidal-colonist/">  <font color="#000080">    <img src="rt.com-on_contact-220405-no_blurb_html_198feb67032166ff.png" name="Image3" alt="On Contact: George Washington and the legacy of white supremacy" align="bottom" width="280" height="157" border="1"/>  </font></a></p><p style="margin-bottom: 0in"><strong><a href="https://www.rt.com/shows/on-contact/550319-george-washington-genocidal-colonist/">OnContact: George Washington and the legacy of white supremacy </a></strong></p><p style="margin-bottom: 0in">On the show, <span class="host">Chris Hedges</span> discusses George Washington, the fallible human being and one of the principalarchitects of the United States, with author Nathaniel Philbrick. AsAmerica fractures into ideologically hostile camps, it colors howwe... </p><p style="margin-bottom: 0in">Feb 25, 2022 09:09 </p><li><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/549103-oppenheimer-bomb-culture-bird/">  <font color="#000080">    <img src="rt.com-on_contact-220405-no_blurb_html_e46c470920b1171d.jpg" name="Image4" alt="On Contact: Oppenheimer & the bomb culture" align="bottom" width="420" height="236" border="1"/>  </font></a></p><p style="margin-bottom: 0in"><strong><a href="https://www.rt.com/shows/on-contact/549103-oppenheimer-bomb-culture-bird/">OnContact: Oppenheimer &amp; the bomb culture </a></strong></p><p style="margin-bottom: 0in">On the show, Chris Hedges discusses J.Robert Oppenheimer and the making of the bomb with author <span class="author">Kai Bird.J. Robert Oppenheimer</span>, &ldquo;the father of the atomic bomb,&rdquo;was by the end of World War II one of the most celebrated men inAmerica.... </p><p style="margin-bottom: 0in">Feb 20, 2022 06:10 </p><li><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/469859-war-iran-stephen-kinzer/">  <font color="#000080">    <img src="rt.com-on_contact-220405-no_blurb_html_15449064d00f77f3.jpg" name="Image149" alt="On Contact – War with Iran? Stephen Kinzer" align="bottom" width="420" height="236" border="1"/>  </font></a></p><p style="margin-bottom: 0in"><strong><a href="https://www.rt.com/shows/on-contact/469859-war-iran-stephen-kinzer/">OnContact &ndash; War with Iran? Stephen Kinzer </a></strong></p><p style="margin-bottom: 0in">Host Chris Hedges talks to journalistand author, Stephen Kinzer, on efforts by Saudi Arabia and Washington to cripple Iran&rsquo;s economy, inevitably putting Saudi Arabia, its Gulf allies and Washington on a collision course with the <em>Islamic</em>... </p><p style="margin-bottom: 0in">Sep 29, 2019 07:10 </p><li><p style="margin-bottom: 0in"><a href="https://www.rt.com/shows/on-contact/469339-future-amazon-rain-forest/">  <font color="#000080">    <img src="rt.com-on_contact-220405-no_blurb_html_b82502a96022a758.png" name="Image150" alt="The future of the Amazon rain forest – Sonia Bone Guajajara" align="bottom" width="280" height="157" border="1"/>  </font></a></p><p style="margin-bottom: 0in"><strong><a href="https://www.rt.com/shows/on-contact/469339-future-amazon-rain-forest/">Thefuture of the Amazon rain forest &ndash; Sonia Bone Guajajara </a></strong></p><p style="margin-bottom: 0in">Host Chris Hedges talks to Sonia BoneGuajajara, leader of 300 indigenous ethnic groups in Brazil, aboutthe future of the Amazon rain forest, its people, climate change,and the competing goals of agrobusiness, multinational corporations,and the... </p><p style="margin-bottom: 0in">Sep 22, 2019 07:15 </p></ul>
regex notepad++
  • 2 2 个回答
  • 73 Views

2 个回答

  • Voted
  1. Toto
    2022-04-06T07:11:55+08:002022-04-06T07:11:55+08:00

    您的正则表达式包含一些错误,使其与文本不匹配。

    • 删除为斜线字符转义的无用(在 Notepad++ 中)\/==>/
    • .*用非贪婪的替换所有你的.*?
    • 您的 Tempered Greedy Token is in wrong order(?:.(?!</p>))+应该是(?:(?!</p>).)+

    此外,<li>示例文本中的 2 没有相同的结构:

    • 前者在<p>第二段中有图像
    • 后者,在<p>第一段中有图像

    那么捕获组不会捕获相同的数据。


    您可以在此处查看正则表达式


    我已经改变了一点你的正则表达式,假设想要的段落不包含任何标签,它适用于你的例子:

    <li>.*?<a href="([^"]+)".*?alt="On Contact: ([^"]+)".*?<p[^>]*>((?:(?![<>]).)+?)</p>.*?<p[^>]*>([a-zA-Z]{3} \d\d?, \d{4} \d\d?:\d\d)\s*</p>
    

    演示和解释


    在记事本++中运行

    • Ctrl+H
    • 找什么:<li>.*?<a href="([^"]+)".*?alt="On Contact: ([^"]+)".*?<p[^>]*>((?:(?![<>]).)+?)</p>.*?<p[^>]*>([a-zA-Z]{3} \d\d?, \d{4} \d\d?:\d\d)\s*</p>
    • 用。。。来代替:$4\n$2\n$1\n$3\n\n
    • 检查 环绕
    • CHECK 正则表达式
    • 查看 . matches newline
    • Replace all

    截图(之前):

    在此处输入图像描述

    截图(之后):

    在此处输入图像描述

    • 1
  2. Best Answer
    OnlineCop
    2022-04-09T07:03:45+08:002022-04-09T07:03:45+08:00

    我喜欢分解问题并尝试优化我发现的.*任何问题。.*?请注意,如果 HTML 的结构发生变化,则破坏的可能性要高得多。

    我也是支持/x标志的正则表达式的粉丝,因此我可以添加空格和注释以帮助所有内容融入我的大脑。

    这就是我想出的,加上评论以帮助理解每个部分在做什么:

    <li>
    (?>[<](?!a\b)[^<>]*[>]|[^<>]+)*
    <a\shref="(?<url>[^"]+)"[^>]*>
    
    # Match until we reach '<img'
    (?>[<](?!img\b)[^<>]*[>]|[^<>]+)*
    <img
    
    # Match until we reach 'alt=' within '<img...>'
    (?>[^<>=]*+(?<!alt)=|"[^<>"=]*"\s)*
    alt="(?:On\sContact[\s–:\-–]*)?(?<on_contact>[^"]+)"[^<>]*>
    
    # Match until it reaches a '<p...>' that does not contain some other opening '<' tag element.
    (?>[<](?!p\b)[^<>]*[>]|[^<>]+|<p[^>]*>\s*<(?!\/?p\b)[^<>]*>)*
    <p[^>]*>
    
    # Match 'stuff stuff ... stuff stuff' without including trailing whitespace.
    (?<desc>[^<>\s]+(?>\s+[^<>\s]+)*
      # Handle <strong>...</strong> nested tags
      (?>\s*[<](?!\/p)[^<>]*[>]|\s*[^<>\s]+(?>\s+[^<>\s]+)*)*
    )
    
    \s*<\/p>
    
    # Match until we reach another '<p...>'
    (?>[<](?!p\b)[^<>]*[>]|[^<>]+)*
    <p[^>]*>
    
    # Capture the date
    (?<date>[^<]+)
    
    # Match until we reach a '<li>' (or end of string)
    (?>[<](?!li\b)[^<>]*[>]|[^<>]+)*
    

    您可以在此处看到这对您的原始文本的作用。

    相同的正则表达式,但去掉了注释行和空格也可以在这里找到,它应该可以直接放入 Notepad++ 或任何你拥有的 PCRE2 兼容工具。

    • 0

相关问题

  • Notepad++ 删除直到冒号替换所有行

  • OneDrive 有 .gitignore 吗?

  • NotePad++ 用户定义语言不显示条件语句

  • 如果一个字符串出现在正则表达式中的另一个字符串之前,如何停止搜索

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve