简体   繁体   English

在记事本中使用REGEX替换字符串的子字符串

[英]Replace substring of a string using REGEX in Notepad++

I am using notepad++ and I want to create an automation in order to replace some strings. 我正在使用notepad ++,并且想要创建自动化以替换一些字符串。

In this case I am going to deal with the a href tag. 在这种情况下,我将处理a href标签。

So, I will give 3 examples of some lines I have in my code : 因此,我将给出3个示例代码示例:

01 ) 01

<a href="https://url.com" class="logo"><img src="urlurlurlurl" alt=""></a>

02 ) 02

<a href="https://url.com" class="logo"><img src="urlurlurlurl" alt="">
         </a>

03 ) 03

<a href="https://url.com"><img src="urlurlurlurl" alt=""></a>

04 ) 04

<a href="https://url.com">link</a>

So, if I wanted to replace the full a href tag above in all 4 cases, I would use this one : <a href(.*?)a> 因此,如果我想在所有4种情况下都替换上面的完整a href标记,则可以使用以下一种: <a href(.*?)a>

Now, I am trying to think of a way to replace the url within the a href tag only. 现在,我正在尝试一种方法来仅替换href标记内的url。

I tried using that : 我试着使用:

href="(?s)(.*?)"|href ="(?s)(.*?)"

and it works fine because I also take into consideration that there might be a space. 而且效果很好,因为我还考虑到可能会有空间。

But now in the replace window I have to include href="" 但是现在在替换窗口中,我必须包含href=""

在此处输入图片说明

Is there a way to make it search for the a href tags and then replace a specific substring of it? 有没有办法让它搜索a href标记,然后替换其特定的子字符串?

I want to know because there are cases where I have other tags that include a url and I want to replace it. 我想知道,因为在某些情况下,我还有其他包含URL的标签,但我想替换它。 But a generic replacement for all the strings that are included within quotes ("string") would not be good as I do not to replace all of them. 但是用引号(“ string”)包含的所有字符串的通用替换都不好,因为我不替换所有的字符串。

You can use a negated class to match everything before and after the href like, 您可以使用否定的类来匹配href前后的所有内容,例如,

(a[^>]*href\s*=\s*")[^"]*

replace with capture group $1REPLACE_STRING 替换为捕获组$1REPLACE_STRING

Regex Demo 正则表达式演示

What it does? 它能做什么?

  • a[^>]* Matches a followed by anything other than a closing > . a[^>]*匹配a后跟除闭合>之外的任何内容。
  • href\\s*=\\s*" Matches href=" . href\\s*=\\s*"匹配href=" Till here is captured in group 1. 直到这里被捕获在组1中。
  • [^"]* Matches anything other than " . [^"]*匹配除"之外的任何内容。 This form the url that you want to replace. 该表格包含您要替换的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM