published · 2 min read

The Regex That Deleted My AI Disclosure From 20 Posts

An optional group in a caption script, and the line it wasn't meant to touch.

The caption regex with its optional group highlighted: two newlines, then an italic line, then a question mark that makes the whole group optional.
The group that took the disclosure line.

An AI agent drafted this from my projects, agents ran the measurements, and another agent checked the draft claim by claim against the code and the run records or briefs that were kept. The projects and opinions are mine.

Medium wants AI help disclosed near the top of a post, so every draft here has an italic line at the very top saying so, under the lead image and caption if it has them. On 6 November a script deleted that line from 20 posts, by my notes from that day, and it went unnoticed for hours. That script was mine, or rather an agent's on my behalf: a small tool that updates image alt text and captions. Captions on my blog are a single italic line under an image, so the pattern for "an image marker, maybe followed by a caption" looked like this:

rf"\[IMAGE: {name} — [^\n]*\](?:\n\n\*[^\n]*\*(?=\n))?"

The optional group takes the line under the image, one blank line down, if it starts and ends with an asterisk, and ? takes it whenever it can. Where a lead image had no caption, the next italic line was the disclosure, and the tool treated it as an old caption and replaced it with the new caption, which was empty. In 20 posts the disclosure vanished, and the edits looked like tidy caption updates. An independent agent reading across posts for contradictions caught the deletion, because another post of mine claims that all my posts carry that line. The originals were in a backup folder, so the repair was simple.

An image marker with no caption under it, then the italic AI disclosure line; the optional group matches the disclosure as if it were an old caption and replaces it with an empty one, and the line is gone.
Where a lead image had no caption, the disclosure looked like one.

One negative lookahead stops the group taking the disclosure, though any other one-line italic or bold paragraph under an image with no caption still goes:

rf"\[IMAGE: {name} — [^\n]*\](?:\n\n\*(?!\[DISCLOSURE)[^\n]*\*(?=\n))?"

I trust the layers around that lookahead more than the lookahead itself. Now the export skips any post without a real disclosure line and says so, and lint blocks it; both used to accept the marker anywhere in the text, and the fixed regex above contains it, so this post would have passed with its disclosure gone.

A test also runs that exact caption update on a sample post. A day later an agent removed the lookahead on purpose in a copy to see whether the test would notice, and it failed as it should.