#regex #regular #spot #print #expression #error #version

bin+lib regexnight

Command-line tool to print syntax-highlighted versions of regular expressions and spot errors

2 releases

new 0.1.1 Nov 21, 2024
0.1.0 Nov 21, 2024

#293 in Text processing

Download history 214/week @ 2024-11-17

214 downloads per month

Apache-2.0

53KB
970 lines

Regex Night

🎶 Regex night, we come together when the code ain't right

🎶 Regex night, and we'll be debuggin' 'til the morning light

Regex Night is a command-line tool to print syntax-highlighted versions of regular expressions and spot errors in them.

Usage

Regex Night main commands are:

  • print
  • lint

Each of these commands let you pass a regular expression either on the command-line, via a file or by reading it from stdin:

regexnight print '(foo|bar)'

echo '(foo|bar)' | regexnight print

echo '(foo|bar)' > a_file
regexnight print -f a_file

Passing a regular expression from the command-line is handy for simple regular expressions, but can get complicated when it includes quotes or backslashes as those have to be properly escaped for the shell to pass them unaltered. In these situations it's less error-prone to write the regular expression in a file or just run the command without argument and type in the regular expression.

Examples

Let's have a look at this regex used to match phone numbers:

^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$

(Source: https://www.variables.sh/complex-regular-expression-examples/)

regexnight print can produce a colored version of it:

regexnight print running

That's nice but still cryptic. We can get a more readable result with the --tree option:

regexnight tree running

Regex Night can also be used to lint regular expressions. Here is a regular expression with an unnecessary extra group:

Hello (?P<name>(?:\w+))!

Running this expression through regexnight lint gives us:

regexnight lint pointing to the extra group

Installation

Build it yourself

If you have Rust installed, run:

cargo install regexnight

Get the binary

You can download a binary from the release page and put it in your PATH.

There are only x86-64 Linux binaries for now.

Known limitations

  • Regex Night currently only supports Python regular expression syntax, because this is what I work with every day. Other syntaxes could be added.
  • Some aspects of the syntax are not properly supported. For example multi-character escapes.
  • There is only one lint check at the moment. More are expected to come.

Dependencies

~4–13MB
~138K SLoC