简体   繁体   English

Notepad ++ RegEx查找/替换指定的组和反向引用

[英]Notepad++ RegEx Find/Replace specified group and backreference

I´ve had some trouble getting my RegEx find/replace working in NPP for hours. 我的RegEx在NPP中找到/替换了好几个小时,遇到了一些麻烦。 Here´s some code out of the files I´m working on: https://regex101.com/r/kQdy4L/6/ My goal is to replace all the "0>.|.|..." by their id name 以下是我正在处理的文件中的一些代码: https ://regex101.com/r/kQdy4L/6/我的目标是用其ID替换所有的“ 0>。|。| ...”名称

test string 测试字符串

<movingPart index="0>8|1|3" referencePoint="0>8|1|0|4" referenceFrame="0>" scaleZ="true"/>
bla
bla
<i3dMapping id="KroneComprimaV180XC" node="0>" />
<i3dMapping id="novoGripPart2_fixPoint" node="0>8|1|0|4" />
<i3dMapping id="novoGrip_part2" node="0>8|1|3" />

substitution 替代

<movingPart index="novoGrip_part2" referencePoint="novoGripPart2_fixPoint" referenceFrame="KroneComprimaV180XC" scaleZ="true"/>
bla
bla
<i3dMapping id="KroneComprimaV180XC" node="0>" />
<i3dMapping id="novoGripPart2_fixPoint" node="0>8|1|0|4" />
<i3dMapping id="novoGrip_part2" node="0>8|1|3" />

After some tial´n´error I got this RegEx 经过一些tial'n´error后,我得到了此RegEx

(".[>].*?")|<i3dMapping id=(?P<name>".*?") node=(".[>].*?")
(".[>].*?")|<i3dMapping id=(?P<name>".*?") node=(".[>].*?")\=1

Which do find either nodes+ids or only the nodes I need to replace however I can´t figure out how to replace all "0>.|.|." 哪一个确实找到了节点+ id或仅找到我需要替换的节点,但是我不知道如何替换所有的“ 0>。|。|”。 with the id name ID名称

Thanks for helping me out, this is the first time I get confronted with RegEx, so this is all very confusing to me. 感谢您的帮助,这是我第一次遇到RegEx,所以这一切都让我感到困惑。 Cheers Fred 干杯弗雷德

You can use this regexp to do the replacements for you: 您可以使用此正则表达式为您进行替换:

(?<=(index|Point|Frame)=")([^"]+)(?=".*?id="([^"]+)" node="\2")

It uses a positive lookbehind for one of index=" , Point=" or Frame=" (note we have to cut the reference off because a lookbehind must be of fixed length), followed by some number of non- " characters (that value being captured in group 2), then a positive lookahead for a string which looks like id="someidvalue" node="\\2" where the \\2 refers to the value captured earlier. 它对index="Point="Frame="之一使用正向后视(注意,我们必须切断reference ,因为后视必须具有固定的长度),后跟一些非"字符(该值(在第2组中捕获),然后对一个看起来像id="someidvalue" node="\\2"的字符串进行正向查找,其中\\2表示较早捕获的值。 The value someidvalue is captured in group 3. someidvalue在组3中捕获。

You can then replace with $3 . 然后,您可以替换为$3 Note that you need to use Replace All , for some reason Notepad++ doesn't like replacing this on a match by match basis. 请注意,由于某些原因,Notepad ++不喜欢逐个Replace All ,因此您需要使用“ Replace All替换”。

Here's a demo of the regex on regex101.com 这是regex101.com上正则表达式的演示

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

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