我有一个多行文字,我想交换莱布尼兹和牛顿的名字:
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.
所以我创建了(在S.O.中的示例的帮助下)以下正则表达式:
(Leibniz|Newton)(.+)(Leibniz|Newton)
替换为
$3$2$1
结果是名称仅在第1行和第3行交换:
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.
我哪里出错了?我只知道我的正则表达式只有在行包含一个A和一个B时才有效,但如果A OR B出现的次数少于(0)或更多(2,3,等等......)则不行。
我会非常感谢一个小小的解释(如果它不是一件苦差事)。
提前致谢