You can wash the worm with PBS buffer, this removes the traces of formalin. And you can find many protocols for DNA Extraction. You can also use the Single cell DNA isolation and amplification protocol to achieve your goals. Only precaution you need to take is that the gut may have the host particles. You need to invest sometime in cleaning it.