Add line number tracking and optimize pattern creation