LinuxCommandLibrary

grex

Generate regular expressions from examples

TLDR

Generate a simple regular expression

$ grex [space_separated_strings]
copy

Generate a case-insensitive regular expression
$ grex [[-i|--ignore-case]] [space_separated_strings]
copy

Replace digits with '\d'
$ grex [[-d|--digits]] [space_separated_strings]
copy

Replace Unicode word character with '\w'
$ grex [[-w|--words]] [space_separated_strings]
copy

Replace spaces with '\s'
$ grex [[-s|--spaces]] [space_separated_strings]
copy

Add {min, max} quantifier representation for repeating sub-strings
$ grex [[-r|--repetitions]] [space_separated_strings]
copy

SYNOPSIS

grex [OPTIONS] [STRING...]

PARAMETERS

--version
    Show program's version number and exit.

--help
    Show program's help message and exit.

-i, --case-insensitive
    Ignore letter case.

-s, --single-line
    Handle strings line by line instead of all strings as one string.

-g, --groups
    Generate non-capturing groups instead of character classes whenever possible.

-w, --word
    Surround the result with word boundaries.

-d, --digits
    Surround every digit sequence with word boundaries.

-e, --escape
    Escape non-ASCII characters.

-I, --input-type
    Specify the input type (plain, json, jsonl).

STRING...
    Strings to generate a regular expression from. If no strings are provided, grex reads from stdin.

DESCRIPTION

grex is a command-line tool that generates regular expressions from user-provided test cases. It's designed to automate the tedious process of crafting regular expressions, especially when dealing with complex or varied input data. The user provides a set of strings, and grex analyzes them to produce a regex that matches all of them. It aims to produce the shortest and simplest regex that can accommodate all the input strings.
grex can handle a variety of input scenarios, including cases with character classes, quantifiers, and alternation. While it excels at creating regexes that match all provided inputs, it doesn't guarantee the regex is the most efficient or the only possible solution. It's a helpful tool for quickly generating regexes that can then be further refined manually if needed. It prioritizes correctness and safety in that it prefers generating overly broad expressions rather than missing parts of the required language. The name grex is derived from "generate regular expression".

CAVEATS

The generated regular expressions may not always be the most optimized or human-readable, and could potentially match unintended input if not carefully reviewed.

INPUT TYPES

grex accepts different input types, including plain text, JSON, and JSON lines. This allows for flexible integration with various data sources.

STDIN USAGE

If no strings are provided as command-line arguments, grex will read strings from standard input (stdin), allowing you to pipe data into the tool.

SEE ALSO

grep(1), sed(1), awk(1)

Copied to clipboard