Search code examples
regexnotepad++substitution

Substitute one group with another group


<P1 x="-0,36935" y="0,26315"/><P2 x="4,29731" y="0,26315"/><P3 x="5,29731" y="-0,40351"/><P4 x="-0,36935" y="-0,40351"/>
<P1 x="4,64065" y="0,26315"/><P2 x="5,97398" y="0,26315"/><P3 x="5,30731" y="-0,40351"/><P4 x="4,64065" y="-0,40351"/>

I want to put a value of P3(x) into P2(x).

So far I have a somewhat working solution, that is not the prettiest;

((?:P2 x=\")(.*?[^\"]+)(?=.+((?:P3 x=\")(.*?[^\"]+))))

It forces me to use P2 x="\4 substitution instead of simply \4

https://regex101.com/r/iua3p0/1

What I am trying is to separate Group 2 from Group 1, and Group 4 from Group 3, that at the same time would allow me to use value of Group 4 in Group 2

Group Substitution


Solution

  • You may try this regex:

    (?<=<P2 x=")[\d,-]+(?=.*<P3 x="([\d,-]+)")
    

    Substitution:

    \1
    
    (?<=<P2 x=")           // positive lookbehind, the following rule must be preceded by <P2 x="
    [\d,-]+                // a set of characters formed by digits, comma and -
    (?=.*<P3 x="([\d,-]+)) // positive lookahead, find the x value of P3 and store it in group 1
    

    See the proof