0

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

ThG
  • 2,361
  • 4
  • 22
  • 33
  • Use lazy dot matching - [`(Leibniz|Newton)(.+?)(Leibniz|Newton)`](https://regex101.com/r/QkpbUq/1). – Wiktor Stribiżew Feb 27 '17 at 12:51
  • @Wiktor Stribizew : thank you : it works. Should I delete the question ? – ThG Feb 27 '17 at 12:58
  • You do not have to, it is OK to keep it. – Wiktor Stribiżew Feb 27 '17 at 12:59
  • @Wiktor Stribizew : thank you. BTW I would never have guessed that the answer you referred to was the place where to look... – ThG Feb 27 '17 at 13:02
  • Yeah, but it would turn out that every second regex answer is "add a `?` after `+`/`*`". It is toooo frequent an issue that people forget about lazy quantifiers. Since the pattern is short and the issue is so evident, it must be a dupe. – Wiktor Stribiżew Feb 27 '17 at 13:03

0 Answers0