For me, legalism is when a moral is viewed/enforced as a rule/law and it is treated as more important than the love of God. This is what the Pharisees were guilty of and Jesus was constantly try to show them.
Specifically in this instance, I was not wanting to stay married because I knew God loved me and it was/is his best for me. I was staying married because I didn’t want to break the rules and disappoint the people who held those rules in high regard.
Both are reasons to stay married, but they come from two totally different motivations.