This phrase explains every thing and putting it in context for 17 years long war in Afghanistan speaks the reality about finest.
Please do tell about how Taliban-led government and defenses fared against US in the opening phase of the war (2 months tops), and for how long Afghanistan remained peaceful to large extent afterwards (2 years). Taliban left-overs melted into Pakistani and Irani tribal sectors, regrouped, and bid their time. When NATO pulled majority of its forces from Afghanistan in 2014, only then Taliban had a clear shot towards resurgence in Afghanistan. Unfortunately for US, the elements it brought to power in Afghanistan proved inept and corrupt, alienating many Afghans in the process. This is why US lost its footing in Afghanistan and Trump administration is considering a negotiated settlement with Taliban. Well, shit happens because dynamics of every region are different.
Nevertheless, Taliban was never the real target to begin with, it was Al-Qaeda Network and where it is now? Oh yes, US neutralized it by 2011.
I was alluding to military prowess of US which is laughably ahead of that of Pakistan as well - recall Operation Neptune Spear in 2011. Not only this but Pakistan Army had to clear Waziristan from terrorists in phases due to capacity-constraints. But US managed two overseas occupations (Iraq and Afghanistan) through the years (2003 - 2011), defeating Iraqi insurgency and Al-Qaeda Network by 2011 across both fronts. Not an easy task to pull-off from operational and logistics standpoint, mind you. No army in the world could cope with this much pressure if in the same boat.
However, Salafi jihadism will not die easily:
https://www.csis.org/analysis/evolution-salafi-jihadist-threat
Why do you think terrorist incidents continue to occur in Afghanistan and Pakistan? Because bad apples continue to thrive in both countries in spite of the efforts to alter the mindset of people across the region. Ideological forces are never easy to cull.
Pit American war-machine against another and US will prevail in days, but it is really 'expensive' to employ against ideological forces. Economic considerations cannot be overlooked.
Fazool baat karney sey pehley soch liya karo.