Skip to main content

Extract Code Blocks

Extract code from Markdown or mixed text content.

Feedback

Key Features

  • Identifies code wrapped in triple backticks (```)
  • Ignores conversational text or explanations
  • Concatenates multiple code blocks if present
  • Preserves indentation within the code

Benefits

  • Grab just the code you need without scrolling through long AI explanations
  • Perfect for developers copying solutions from Stack Overflow or LLMs
  • Save time manually selecting code regions
  • Get a clean file ready to be saved

How to use this tool

Paste the entire AI conversation or response containing code

Click process

Only the content inside code blocks will be extracted to the output

Pulling code out of a conversation without losing the indentation

Extracting code blocks is the inverse of stripping Markdown: keep what is inside the fences and discard everything around it. The value is in the second half, because the prose around code in a chat transcript or a documentation page is usually longer than the code itself and entirely unwanted when you are pasting into an editor.

Fenced blocks, the ones delimited by triple backticks, are unambiguous and extract reliably. Many carry a language hint on the opening fence, which is worth keeping if you intend to save each block to a correctly named file. Indented code blocks, the older convention of four leading spaces, are harder: four spaces is also how people indent a nested list, so a purely indentation-based rule will occasionally extract prose and occasionally miss code.

Indentation inside the block is the thing that must survive untouched, and it is the thing casual copying most often breaks. In Python it is syntax rather than style, so a lost level is a program that no longer runs. Tabs converted to spaces, or spaces normalised to a different width, change files in ways a diff will show and a reviewer will not thank you for. Extraction should copy the block byte for byte and do nothing clever.

Multiple blocks raise a question worth answering before you start: do you want them concatenated into one file, or kept separate. A chat that built a program step by step usually wants them in order as one file. A page documenting several independent examples usually wants them apart, since running them together makes no sense.

One thing to check by eye: chat interfaces sometimes insert invisible characters or convert straight quotes to curly ones inside what they display as code. Curly quotes are not valid string delimiters in any language, and the resulting syntax error points at a line that looks perfectly correct. If extracted code fails to parse and the line looks fine, that is the first thing to suspect.

The extraction runs in your browser, which is the point when the transcript you are pulling from contains an API key, an internal hostname or a customer identifier in an example, as transcripts routinely do.

Frequently Asked Questions

What if there is no code in the text?

The tool will return an empty result or a message indicating no code blocks were found.

Related tools

Need this in a script?

The same tool answers on HTTP. Free, 1,000 calls a day, no card, and one token works across every Astral API.

POST https://astraltext.com/api/v1/extract-code-blocks

Extract code blocks APIGet a free token