Was it alive & wiggling?
It looks to me like a lung plug, which normally occurs during lung congestion. When coughed up, they look exactly like worms, but they are just thickened phlegm that was plugging up a lung bronchial tube. The key is to watch it carefully to see if it wiggles. Then it is a worm.
I used to get a lot of lung plugs when I was suffering from asthma.