简体   繁体   English

Notepad ++用RegEx搜索结果替换文本

[英]Notepad++ replace text with RegEx search result

I would like replace a standard string in a file, with another that is a result of a regular expression. 我想用正则表达式的结果替换另一个文件中的标准字符串。 The standard text looks like: 标准文本如下所示:

<xsl:variable name="ServiceCode" select="###"/>

I would like to replace ### with a servicecode, that I can find later in the same file, from this URL: 我想将###替换为服务代码,稍后可以从该URL在同一文件中找到:

<a href="/Services/xyz" target="_self">

The regular expression (?<=\\/Services\\/)(.*)(?=\\" ) returns the required service code "xyz". 正则表达式(?<=\\/Services\\/)(.*)(?=\\" )返回所需的服务代码” xyz“。

So, I opened Notepad++, added "###" to the "Find what" and this RegEx to the "Replace with" section, and expected that the ### text will be replaced by xyz . 因此,我打开了Notepad ++,在“查找内容”中添加了“ ###”,在“替换为”部分中添加了此RegEx,并期望将###文本替换为xyz But I got this result: 但是我得到了这个结果:

<xsl:variable name="ServiceCode" select="?<=/Services/.*?=" "/>

I am new to RegEx, do I need to use different syntax in the replace section than I use to find a string? 我是RegEx的新手,在替换部分中是否需要使用与查找字符串不同的语法? Can someone give me a hint how to achieve the required result? 有人可以提示我如何达到所需的结果吗? The goal is to standardize tons of files with similar structure as now all servicecodes are hardcoded in several places in the file. 目的是使大量具有相似结构的文件标准化,因为现在所有服务代码都在文件中的多个位置进行了硬编码。 Thanks. 谢谢。

You could use a lookahead for capturing the part ahead. 你可以使用一个前瞻捕捉未来的一部分。

Search for: (?s)###(?=.*/Services/([^"]+)") and replace with: $1 搜索:( (?s)###(?=.*/Services/([^"]+)")并替换为: $1

  • (?s) makes the dot also match newlines (there is also a checkbox available in np++) (?s)使点也与换行符匹配(np ++中还有一个复选框)
  • [^"] matches a character that is not " [^"]匹配的字符不是 "

The replacement $1 corresponds to capture of first parenthesized subpattern. 替换$1对应于捕获第一个带括号的子模式。

I am no expert at RegEx but I think I may be able to help. 我不是RegEx的专家,但我认为我可以提供帮助。 It looks like you might be going at this the wrong way. 看来您可能会走错路了。 The regex search that you are using would normally work like this: 您正在使用的正则表达式搜索通常会像这样工作:

The parenthesis () in RegEx allow you to select part of your search and use that in the replace section. RegEx中的括号()允许您选择搜索的一部分,并在替换部分中使用。

You place (?<=\\/Services\\/)(.*)(?=\\" ) into the "Find what" section in Notepad++. 您将(?<=\\/Services\\/)(.*)(?=\\" )放入Notepad ++中的“查找内容”部分。

Then in the "Replace with" section you could use \\1 or \\2 or \\3 to replace the contents of your search with what was found in the (?<=\\/Services\\/) or (.*) or (?=\\" ) searches respectively. 然后,在“替换为”部分中,可以使用\\1\\2\\3将搜索内容替换为(?<=\\/Services\\/)(.*)(?=\\" )分别搜索。

Depending on the structure of your files, you would need to use a RegEx search that selects both lines of code (and the specific parts you need), then use a combination of \\1\\2\\3 etc. to replace everything exactly how it was, except for the ### which you could replace with the \\number associated with xyz . 根据文件的结构,您需要使用RegEx搜索来选择两行代码(以及所需的特定部分),然后使用\\1\\2\\3等的组合来完全替换所有内容是,除了###,您可以将其替换为与xyz关联的\\number

See http://docs.notepad-plus-plus.org/index.php/Regular_Expressions for more info. 有关更多信息,请参见http://docs.notepad-plus-plus.org/index.php/Regular_Expressions

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

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