I’ve struggled with the same issue and it took years before I could stop.
One thing I did learn, however, was no matter how “resolved” I was to not give in, it almost made the problem harder. Then, I read the Diary of St. Faustina, and there was a part in it where she resolved not to fall into some minor imperfection that day. But, after making the resolution, she fell into it twice as much. Later, she asked Christ why she fell more when she was so resolute not to fall into this imperfection. He replied to her that she fell more because she made the resolution without Him and tried to do it on her own.
This opened up a new understanding for me, not just in the case of impurity, but with every sin, that I cannot overcome sin by myself no matter how resolved I am. I must, I must, I MUST have Christ help me… and His mother
Give this struggle to Him. You may still struggle with it, and that’s okay. But know that it is in His hands now and you are not struggling with this alone. He loves you more than you can know and He loves us in our weaknesses… yes, even this weakness, and He loves us even more when we give our weaknesses to Him. Part of giving your weaknesses to Christ is giving it to others, namely, through the sacrament of Confession.
I used to treat Confession (and sadly still often do) as something that I had to get over with. “Oh great, I masturbated again, now I have to humiliate myself by telling this to a priest.” No matter how nice the priest was or how good his advice may have been, I’d leave the confessional all in a tizzy and so extremely embarrassed that I felt worse leaving than going in. What I came to realize is that my attitude towards Confession was wrong. Confession isn’t a place where I have to “own up” to my sins or go to hell, but it was a place of true reconciliation and healing.
You’ve already repented of this sin… you didn’t even really “want” to fall into it in the first place, but like everyone, you fell because of human weakness. The next time you confess, really try, as best you can, to offer this weakness to Christ. Tell Him you can’t do it alone and that you love Him so much. It’s not about your failure but about His love for you!
You are not alone and this is a struggle for many. I tell you it IS possible to overcome this even if it seems the struggle will never end.
A quick suggestion the next time you are faced with this temptation is, don’t dwell on the temptation. Relax and give it to Christ and Mary immediately. Trying to fight it just gave me more anxiety and made the struggle harder. It’s okay to acknowledge that the temptation is there and then give it away. Find something interesting to do to distract yourself in the meantime.
Don’t get discouraged. There is always hope!
Praying for you 
