将一组替换为另一组
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"/>
我想将 P3(x) 的值放入 P2(x)。
到目前为止我有一个可行的解决方案,但不是最漂亮的;
((?:P2 x=\")(.*?[^\"]+)(?=.+((?:P3 x=\")(.*?[^\"]+))))
它迫使我使用 P2 x="
替换而不是简单地
https://regex101.com/r/iua3p0/1
我正在尝试的是将第 2 组与第 1 组分开,将第 4 组与第 3 组分开,
同时允许我在第 2 组中使用第 4 组的值
您可以试试这个正则表达式:
(?<=<P2 x=")[\d,-]+(?=.*<P3 x="([\d,-]+)")
替换:
(?<=<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
<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"/>
我想将 P3(x) 的值放入 P2(x)。
到目前为止我有一个可行的解决方案,但不是最漂亮的;
((?:P2 x=\")(.*?[^\"]+)(?=.+((?:P3 x=\")(.*?[^\"]+))))
它迫使我使用 P2 x="
替换而不是简单地
https://regex101.com/r/iua3p0/1
我正在尝试的是将第 2 组与第 1 组分开,将第 4 组与第 3 组分开, 同时允许我在第 2 组中使用第 4 组的值
您可以试试这个正则表达式:
(?<=<P2 x=")[\d,-]+(?=.*<P3 x="([\d,-]+)")
替换:
(?<=<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