“Mary, Mary, Mary” … repeat these words if you’re unsure; for whosoever “remembers having invoked the name of Mary may be sure that he has not violated chastity.” (St. Alphonsus Liguori)
The same saint has these words of consolation for those with scruples or a tendency to scrupulosity:
"St. Teresa well said, “Let the soul accept the confessor with a determination to think no more of personal excuses, but to trust in the words of the Lord, ‘He that heareth you heareth Me’. The Lord so highly values this submission, that when, in spite of a thousand inward conflicts, and considering the decision to be an absurd one, we execute it nevertheless, cost us what it may, the Lord so assists us,” etc.; and she goes on to say, that we then comply with his divine will.
Hence St. Francis de Sales, speaking of direction from a spiritual Father in order to walk securely in the way of God, says, “This is the very counsel of all counsels.” “Search as much as you will,” says the devout Avila, “you will in no way discover the will of God so surely as by the path of that humble obedience which is so much recommended and practised by the devout of former times.” Thus, too, Alvarez said, “Even if the spiritual Father should err, the obedient soul is secure from error, because it rests on the judgment of him whom God has given it as a superior.” And F. Nieremberg writes to the same effect: “Let the soul obey the confessor; and then, although the thing itself were matter of fault, he does not sin who does it with the intention of obeying him who holds to him the place of God, persuading himself (as is, indeed, the case) that he is bound to obey him;” forasmuch as (according to the words of F. Rogacci and F. Lessius) the confessor is to us the interpreter of the [p. 454] divine will. And this is confirmed also by the gloss: “But if what is prescribed be of a doubtful kind, the virtue of obedience exempts from sin, although the thing in itself be evil;” and in the chapter Inquisition de Sent, exc., from the same text, obedience to the confessor is enjoined, when it says that scruples “ought to be dismissed at the judgment of one’s pastor.” St. Francis de Sales gives three maxims of great consolation to the scrupulous: “An obedient soul has never been lost; 2. We ought to rest satisfied with knowing from our spiritual Father that we are going on well, without seeking a personal knowledge of it; 3. The best thing is to walk on blindly through all the darkness and perplexity of this life, under the providence of God.”
God bless.