【问题标题】:Regex Notepad ++: How to remove everything except url?正则表达式记事本 ++:如何删除除 url 之外的所有内容?
【发布时间】:2014-12-15 14:34:50
【问题描述】:

我有这样的站点地图:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
    <loc>http://mywebsite.com/article1</loc>
    <lastmod>2014-08-10</lastmod>
    <changefreq>monthly</changefreq>
  </url>
  <url>
    <loc>http://mywebsite.com/article2</loc>
    <lastmod>2014-08-10</lastmod>
    <changefreq>monthly</changefreq>
  </url>
  <url>
    <loc>http://mywebsite.com/article3</loc>
    <lastmod>2014-08-10</lastmod>
    <changefreq>monthly</changefreq>
  </url>
</urlset>

我只想保留里面的 url。你知道如何匹配其他人并且什么都没有替换吗?非常感谢!

【问题讨论】:

标签: regex url notepad++ sitemap


【解决方案1】:

如果你想要的结果是这样的:

http://mywebsite.com/article1
http://mywebsite.com/article2
http://mywebsite.com/article3

搜索:

\h*<url\b.*?(http[^<]+).*?</url>|<.*?>\s*

并替换为捕获的 url(在第一个括号组中捕获)

\1

\h 匹配任何水平空格,[^&lt;]+ 匹配一个或多个不是&lt; 的字符

一定要勾选. matches \r and \n复选框

另见example and explanation on regex101.com

【讨论】:

  • 感谢您提供完美的正则表达式和出色的解释!但是,我编辑了我的帖子。你能帮我编辑正则表达式吗?
  • @Loong 查看更新或仅进行第二次替换:剩余 &lt;.*?&gt;\s* 为空。
  • 你好,当我尝试使用记事本++时,它就像一个魅力。但是,当我使用 Find in Files 处理多个文件时,它不起作用。你知道为什么吗?
  • @Loong 你检查了. matches \r and \n / 相同的选项吗?其他我不知道。
【解决方案2】:

您似乎打算匹配元素内部的内容。多行正则表达式匹配内容可以完成这项工作:(http.*)

【讨论】:

  • 不,我想保留 URL,而不是删除它
【解决方案3】:

您可以使用此正则表达式匹配除 URL 之外的所有内容并替换为任何内容:

.*<url>.*\n?.*<loc>|<\/loc>(.*\n?){4}<\/url>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 2010-10-16
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多