Use pattern matching (Regular Expressions) or language-specific heuristics to identify code snippets. Code snippets often have distinct patterns, such as indentation or the presence of common programming keywords.
Or easier, if your LLM allows it, give it instructions to initiate and end your code with a marker - everything inbetween those markers is code.
Another HN user posted this recently:
"Show HN: Get your entire ChatGPT history in Markdown files"[1][2]
Perhaps you can learn a few things from studying the code.
[1]https://news.ycombinator.com/item?id=37636701
[2]https://github.com/mohamed-chs/chatgpt-history-export-to-md