【问题标题】:Regex - Find and replace an url inside href attribute正则表达式 - 在 href 属性中查找和替换 url
【发布时间】:2017-07-30 23:36:14
【问题描述】:

我有一个 xlsx/csv 文件,我正在尝试使用 notepad++ 修改它的内容。 正好是href里面的一个url。例如:

href=""/xs_db/DOKUMENT_DB/www/Datenblaetter/de/7/7521_Datasheet--de.pdf""
href=""/xs_db/DOKUMENT_DB/www/Datenblaetter/de/7609_Datasheet--de.pdf""
href=""/xs_db/DOKUMENT_DB/www/Datenblaetter/de/6/7981_Datasheet--de.pdf""
etc...

替换后,我希望它们看起来像这样:

href=""/docs/7521_Datasheet--de.pdf""
href=""/docs/7609_Datasheet--de.pdf""
href=""/docs/7981_Datasheet--de.pdf""

现在,我在 find 上有这个模式:

(?<=href=(""|''))[^"']+(?=(.pdf""|.pdf''))

编辑: 尝试给定示例后,没有字符串匹配。这是完整的单元格文本:

"<table cellspacing=""0"" width=""100%"" border=""0"" cellpadding=""10""><tbody><tr>
 <td align=""left"" valign=""top"">
 <table cellspacing=""0"" width=""100%"" border=""0"" cellpadding=""0""><tbody><tr>
 <td>
 <table cellspacing=""0"" width=""100%"" border=""0"" cellpadding=""0""><tbody><tr>
 <td align=""left"" valign=""top"" class=""DocRepCell1""><img src=""/catalog/pdf.gif"" alt="" "" border=""0""></td>
 <td align=""left"" width=""97%"" valign=""middle"" class=""DocRepCell2""><span class=""NavigationButtonMoreInfos"">Produktinformation breite</span> </td>
 <td align=""right"" width=""1%"" nowrap=""nowrap"" valign=""middle"" class=""DocRepCell3"">0,1 MB</td>
 <td align=""right"" width=""1%"" nowrap=""nowrap"" valign=""middle"" class=""DocRepCell4"">
  <a class=""NavigationButtonMoreInfos"" target=""_blank"" href=""/xs_db/DOKUMENT_DB/www/Datenblaetter/de/7/7521_Datasheet--de.pdf"">herunterladen</a></td></tr>
  </tbody></table></td></tr></tbody>
  </table></td></tr>
  </tbody></table></td></tr>
  </tbody></table>"

【问题讨论】:

    标签: regex csv notepad++ xlsx findandmodify


    【解决方案1】:

    您可以在正则表达式模式下尝试以下查找和替换:

    查找:

    ^href=""/.*?(\d+_Datasheet.*\.pdf"")$
    

    替换:

    href=""/docs/$1
    

    请注意,如果查找模式不适用于您的更多数据,则可以使其更通用。但总的来说,我们需要一些具体的方法来识别您希望在匹配中保留的后缀的开头。如果我的回答对您不起作用,请说明它失败的地方并提供允许识别后缀的逻辑。

    【讨论】:

    • 这个^ 应该在替换中吗?
    【解决方案2】:

    这是一种仅将您要替换的部分与路径 /docs 匹配的方法

    查找内容:

    ^href=["']+\K(/.*?)(?=/\d+_[\w-]+\.pdf["']+$)
    

    替换为:

    /docs
    

    搜索模式:正则表达式(最好勾选“.matches new lines”)

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 1970-01-01
      • 2014-12-30
      • 2011-07-09
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      相关资源
      最近更新 更多