Extract all matches of a regex pattern
.* RegexExtract phone numbers from text
$ echo "Call 555-1234 or 555-5678
Fax: 555-0000" | regex-extract (input, opts = {}) => {
if (!opts.pattern) return "Error: provide --pattern";
try {
const re = new RegExp(opts.pattern, "gm");
const matches = [...input.matchAll(re)];
if (!matches.length) return "(no matches)";
return matches
.map(
(m, i) =>
`${i + 1}: ${m[0]}${m.length > 1 ? ` → groups: [${m.slice(1).join(", ")}]` : ""}`,
)
.join("\n");
} catch (e) {
return `Error: ${e.message}`;
}
}