我如何找到&用Notepad ++和正则表达式替换url?

时间:2015-12-28 19:23:24

标签: php regex url replace notepad++

我现在已经在搜索了一段时间,但我还没有找到如何做我想做的事情。

我需要搜索文件夹并找到包含带有特定基本网址的href标记的文件。我用以下正则表达式完成了这个:

(href="(https:\/\/www\.mytesturl\.com))

找到使用此URL的文件和位置后,我需要对找到的文本进行替换。这是我的问题所在。 href属性肯定包含文本:

https://www.mytesturl.com

此外,它可能包含任何形式的查询字符串值或" /"之后的路径。

最终,我的查找/替换操作需要产生结果:

href='<%= Request.Url.Scheme + "://" + Request.Url.Host + "<extra>" %>'

<extra>的所有内容都来自&#34; .com&#34;到引号中的初始href值的末尾。

所以

  

https://www.mytesturl.com?somevar=somevalue&secondvar=secondvalue

将是:

  

href =&#39;&lt;%= Request.Url.Scheme +&#34;://&#34; + Request.Url.Host +   &#34;?somevar = someValue中&安培; secondvar = secondvalue&#34; %GT;&#39;

https://www.mytesturl.com/otherpath?somevar=somevalue&secondvar=secondvalue

将是:

  

href =&#39;&lt;%= Request.Url.Scheme +&#34;://&#34; + Request.Url.Host +   &#34;?/ otherpath somevar = someValue中&安培; secondvar = secondvalue&#34; %GT;&#39;

Notepad ++可以执行正则表达式查找/替换吗?

1 个答案:

答案 0 :(得分:1)

您已经遇到了一些问题,当您不应该使用正则表达式时,它们都源于使用正则表达式。给自己写一个小脚本来迭代目录,解析每个HTML文件,导航DOM以查找a标签并检查他们的href属性......然后重写它们(对于那个< / em>你可以使用正则表达式!)。

如果你没有假阴性,但是(有些事件没有找到),那么你可以这样做......使用捕获和反向引用。

所以,你可以搜索:

href="https:\/\/www\.mytesturl\.com([^"]*)"
//                                 ^^^^^^^
//                             optional capture
//                         any characters until '"'

并将其替换为:

href='<%= Request.Url.Scheme + "://" + Request.Url.Host + "\1" %>'
//                                                         ^^
//                                                 contents of capture
//                                               (which may be nothing!)

顺便说一句,你真的应该使用&而不是+来进行ASP中的字符串连接。

此外,“查找”主题上的Notepad ++手册(按F1)说明应用程序使用Scintilla正则表达式引擎,并链接到the Scintilla documentation,这是此类工作的非常方便的参考。 请务必阅读文档。