ISIL and Hamas though are not the same. They are much at odds with each other on agendas and how to carry them out. But you are right, many individuals have fought with the other group. Extremists in Hamas often join ISIL, and less extreme fighters in ISIL often come to Hamas.
That seems to be the main problem. Winning the initial fight is doable and almost everyone succeeds at it. But building a lasting peace afterwards is where counterinsurgencies tend to fall completely apart.
The soviets tried to win Afghanistan. But their strategy was to “kill everything that moves and hope for the best.”