LeaVerou / brep

Write batch find & replace scripts that transform files with a simple human-readable syntax
12 stars 0 forks source link

A way to extract matches only and write them to another file #14

Open LeaVerou opened 1 month ago

LeaVerou commented 1 month ago

Instead of replacing matches and keeping whatever is in between, I’ve often wanted to extract matches and write them to a file. I wonder if that could be in scope as well.

LeaVerou commented 1 month ago

It just occurred to me that the only difference between what brep is currently doing and what this issue is proposing is what happens with the non-matches.

In the (current) replace mode, strings that don’t match any replacement are left intact. When extracting, you only want the strings that matched, and thus want to replace the non-matches with either whitespace or some other separator string (e.g. line breaks, or ------- lines etc).

So perhaps the only primitive we need to add to support this use case is a way to control what non-matches are replaced with defaulting to $& (no change).

E.g. extracting all TODO comments into a Markdown task list could look like this (assuming gaps as the name just for demo purposes):

from: '<!-- TODO (.+?) -->'
to: '- [ ] $1'
gaps: '\n- [ ] '

This could even be a recursive replacement option, no need to only support it at the root level.

Not sure what to call it though. gaps is too vague. gaps_to a bit awkward. non_matches? Equally vague.