I have a multiline text, in which I want to swap the names of Leibniz and Newton :
1) Either Leibniz or Newton or both have invented calculus.
2) Newton claimed he was the first.
3) Newton lived in England, and Leibniz lived in Germany.
4) Leibniz and Newton are the same person, said Leibniz..
5) Leibniz and Newton are not the same person, said Newton about Leibniz.
So I created (with help of examples in S.O.) the following regex :
(Leibniz|Newton)(.+)(Leibniz|Newton)
to be replaced with
$3$2$1
The result is that the names are swapped in lines 1 and 3 only :
1) Either Newton or Leibniz or both have invented calculus. (OK)
2) Newton claimed he was the first.
3) Leibniz lived in England, and Newton lived in Germany. (OK)
4) Leibniz and Newton are the same person, said Leibniz..
5) Leibniz and Newton are not the same person, said Newton about Leibniz.
Where did I go wrong ? I only understand that my regex works only if the lines contain one A AND one B, but not if there are less (0) or more (2, 3, etc...) occurrences of A OR B.
I would be very grateful for a small explanation (if it is not a chore).
Thanks in advance