Search code examples
regexnotepad++substitution

Substitute one group with another group; Followup question


I'll be referring to this thread: Substitute one group with another group

What I'd like to do is to put a value of P1(y) in to P4(y),
with end result: (...) <P4 x="-0,36935" y="0,26315"/>

setting values of groups

My previous question, being similar, seems to require a completely new approach. And unfortunately I couldn't find a reliable solution.

Example to work on: https://regex101.com/r/iua3p0/2

<P1 x="-0,36935" y="0,26315"/><P2 (...)/><P3 (..)/><P4 x="-0,36935" y="-0,40351"/>
<P1 x="4,64065" y="0,26315"/><P2 (...)/><P3 (..)/><P4 x="4,64065" y="-0,40351"/>

Solution

  • To put a value of P1(y) in to P4(y) on the same line, you could use:

    <P1[^>]*\hy="([^"]+)"[^>]*>.*?<P4[^>]*\hy="\K[^"]+(?=[^>]*>)
    

    The pattern matches:

    • <P1[^>]* Match <P1 and optional chars other than >
    • \hy=" Match a space and y="
    • ([^"]+) Capture chars other than " in group 1
    • "[^>]*> Match " and optional chars other than > and then match >
    • .*? Match as few as possible chars
    • <P4[^>]*\hy=" Match <P4 and optional char other than > and then match a space and y="
    • \K[^"]+ Clear the match buffer, and then match what you want to remove, in this case 1+ chars other than "
    • (?=[^>]*>) Positive lookahead to assert a > to the right

    And replace with group 1 using $1

    See a regex demo.

    enter image description here

    Note that to not match across lines using the negated character class, you can exclude matching newlines using [^"\r\n] and [^>\r\n]*