Replace using RegEx in notepad++

爱⌒轻易说出口 提交于 2019-12-24 10:38:08

问题


I would like to replace all names in an XML file tagged by with let's say xyz. In other words, replace everything (including whitespace) between the tags. What am I doing wrong?

Search: (<name>)(.*)(</name>)
Replace: \1xyz\3

回答1:


You are trying to parse XML with regular expressions.

However, what you are doing wrong anyway is using a greedy repetition. This will go all the way from the first <name> to the very last </name> (even if they do not belong together), because the .* will try to consume as much as possible while still fulfilling the match condition.. Use this instead:

Search: (<name>).*?(</name>)
Replace: \1xyz\2

Or to be on the safe side you can also escape the < and >, since they are meta-characters in some specific cases (not in this one though):

Search: (\<name\>).*?(\</name\>)
Replace: \1xyz\2

In both cases, this makes the .* ungreedy, i.e. it will consume as little as possible.

And make sure you upgrade to Notepad++ 6, because before that there were a few issues with Notepad++'s regex engine.

Lastly, as hoombar pointed out in a comment . by default matches every character except line break characters. In Notepadd++ you can change this behavior by ticking the . matches newline checkbox.



来源:https://stackoverflow.com/questions/13195217/replace-using-regex-in-notepad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!