How to add rewrite rule for product compare page?

Here you go:

compare\/([^-vs-]*)(?:-vs-)([^-vs-]*)(?:-vs-)?([^-vs-]*)?

Remember to escape backslashes (as I have done). I also made your lookups more consistent in their internal logic/structure.

I assumed you wouldn’t want a second -vs- if the third product wasn’t there, so this is why you now have the second non-capturing group.

Here is a link with the answer in regex: https://regexr.com/3u465 to make things clear. Please let me know if I misunderstood your requirements. 🙂