pipr.tools

regex-extract

Extract all matches of a regex pattern

.* Regex

Try it

stdin0 chars
stdout0 chars

Flags

--pattern string default:

Example

Extract phone numbers from text

Usage
$ echo "Call 555-1234 or 555-5678
Fax: 555-0000" | regex-extract
View source
(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}`;
      }
    }

Suggested Pipelines

Related Tools