grex is a library as well as a command-line utility that is meant to simplify the often complicated and tedious task of creating regular expressions. It does so by automatically generating a single regular expression from user-provided test cases. The resulting expression is guaranteed to match the test cases which it was generated from.
This project has started as a Rust port of the JavaScript tool regexgen written by Devon Govett. Although a lot of further useful features could be added to it, its development was apparently ceased several years ago. The plan is now to add these new features to grex as Rust really shines when it comes to command-line tools. grex offers all features that regexgen provides, and more.
The philosophy of this project is to generate the most specific regular expression possible by default which exactly matches the given input only and nothing else. With the use of command-line flags (in the CLI tool) or preprocessing methods (in the library), more generalized expressions can be created.
The produced expressions are Perl-compatible regular expressions which are also compatible with the regular expression parser in Rust's regex crate. Other regular expression parsers or respective libraries from other programming languages have not been tested so far, but they ought to be mostly compatible as well.
Definitely, yes! Using the standard settings, grex produces a regular expression that is guaranteed
to match only the test cases given as input and nothing else.
This has been verified by property tests.
However, if the conversion to shorthand character classes such as \w
is enabled, the resulting regex matches
a much wider scope of test cases. Knowledge about the consequences of this conversion is essential for finding
a correct regular expression for your business domain.
grex uses an algorithm that tries to find the shortest possible regex for the given test cases. Very often though, the resulting expression is still longer or more complex than it needs to be. In such cases, a more compact or elegant regex can be created only by hand. Also, every regular expression engine has different built-in optimizations. grex does not know anything about those and therefore cannot optimize its regexes for a specific engine.
So, please learn how to write regular expressions! The currently best use case for grex is to find an initial correct regex which should be inspected by hand if further optimizations are possible.
{min,max}
quantifier notation|
operator?
quantifier^
and $
You can download the self-contained executable for your platform above and put it in a place of your choice. Alternatively, pre-compiled 64-Bit binaries are available within the package managers Scoop (for Windows), Homebrew (for macOS and Linux), MacPorts (for macOS), and Huber (for macOS, Linux and Windows). Raúl Piracés has contributed a Chocolatey Windows package.
grex is also hosted on crates.io, the official Rust package registry. If you are a Rust developer and already have the Rust toolchain installed, you can install by compiling from source using cargo, the Rust package manager. So the summary of your installation options is:
( brew | cargo | choco | huber | port | scoop ) install grex
In order to use grex as a library, simply add it as a dependency to your Cargo.toml
file:
[dependencies] grex = { version = "1.4.5", default-features = false }
The dependency clap is only needed for the command-line tool. By disabling the default features, the download and compilation of clap is prevented for the library.
Detailed explanations of the available settings are provided in the library section. All settings can be freely combined with each other.
Test cases are passed either directly (grex a b c
) or from a file (grex -f test_cases.txt
).
grex is able to receive its input from Unix pipelines as well, e.g. cat test_cases.txt | grex -
.
The following table shows all available flags and options:
$ grex -h
grex 1.4.5
© 2019-today Peter M. Stahl <pemistahl@gmail.com>
Licensed under the Apache License, Version 2.0
Downloadable from https://crates.io/crates/grex
Source code at https://github.com/pemistahl/grex
grex generates regular expressions from user-provided test cases.
Usage: grex [OPTIONS] {INPUT...|--file <FILE>}
Input:
[INPUT]... One or more test cases separated by blank space
-f, --file <FILE> Reads test cases on separate lines from a file
Digit Options:
-d, --digits Converts any Unicode decimal digit to \d
-D, --non-digits Converts any character which is not a Unicode decimal digit to \D
Whitespace Options:
-s, --spaces Converts any Unicode whitespace character to \s
-S, --non-spaces Converts any character which is not a Unicode whitespace character to \S
Word Options:
-w, --words Converts any Unicode word character to \w
-W, --non-words Converts any character which is not a Unicode word character to \W
Escaping Options:
-e, --escape Replaces all non-ASCII characters with unicode escape sequences
--with-surrogates Converts astral code points to surrogate pairs if --escape is set
Repetition Options:
-r, --repetitions
Detects repeated non-overlapping substrings and converts them to {min,max} quantifier
notation
--min-repetitions <QUANTITY>
Specifies the minimum quantity of substring repetitions to be converted if --repetitions
is set [default: 1]
--min-substring-length <LENGTH>
Specifies the minimum length a repeated substring must have in order to be converted if
--repetitions is set [default: 1]
Anchor Options:
--no-start-anchor Removes the caret anchor `^` from the resulting regular expression
--no-end-anchor Removes the dollar sign anchor `$` from the resulting regular expression
--no-anchors Removes the caret and dollar sign anchors from the resulting regular
expression
Display Options:
-x, --verbose Produces a nicer-looking regular expression in verbose mode
-c, --colorize Provides syntax highlighting for the resulting regular expression
Miscellaneous Options:
-i, --ignore-case Performs case-insensitive matching, letters match both upper and lower case
-g, --capture-groups Replaces non-capturing groups with capturing ones
-h, --help Prints help information
-v, --version Prints version information
Test cases are passed either from a collection via RegExpBuilder::from()
or from a file via RegExpBuilder::from_file()
.
If read from a file, each test case must be on a separate line. Lines may be ended with either a newline \n
or a carriage
return with a line feed \r\n
.
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["a", "aa", "aaa"]).build(); assert_eq!(regexp, "^a(?:aa?)?$");
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["a", "aa", "123"]) .with_conversion_of_digits() .with_conversion_of_words() .build(); assert_eq!(regexp, "^(\\d\\d\\d|\\w(?:\\w)?)$");
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["aa", "bcbc", "defdefdef"]) .with_conversion_of_repetitions() .build(); assert_eq!(regexp, "^(?:a{2}|(?:bc){2}|(?:def){3})$");
By default, grex converts each substring this way which is at least a single character long and which is subsequently repeated at least once. You can customize these two parameters if you like.
In the following example, the test case aa
is not converted to a{2}
because the repeated substring
a
has a length of 1, but the minimum substring length has been set to 2.
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["aa", "bcbc", "defdefdef"]) .with_conversion_of_repetitions() .with_minimum_substring_length(2) .build(); assert_eq!(regexp, "^(?:aa|(?:bc){2}|(?:def){3})$");
Setting a minimum number of 2 repetitions in the next example, only the test case defdefdef
will be
converted because it is the only one that is repeated twice.
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["aa", "bcbc", "defdefdef"]) .with_conversion_of_repetitions() .with_minimum_repetitions(2) .build(); assert_eq!(regexp, "^(?:bcbc|aa|(?:def){3})$");
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["You smell like 💩."]) .with_escaping_of_non_ascii_chars(false) .build(); assert_eq!(regexp, "^You smell like \\u{1f4a9}\\.$");
Old versions of JavaScript do not support unicode escape sequences for the astral code planes
(range U+010000
to U+10FFFF
). In order to support these symbols in JavaScript regular
expressions, the conversion to surrogate pairs is necessary. More information on that matter
can be found here.
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["You smell like 💩."]) .with_escaped_non_ascii_chars(true) .build(); assert_eq!(regexp, "^You smell like \\u{d83d}\\u{dca9}\\.$");
The regular expressions that grex generates are case-sensitive by default. Case-insensitive matching can be enabled like so:
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["big", "BIGGER"]) .with_case_insensitive_matching() .build(); assert_eq!(regexp, "(?i)^big(?:ger)?$");
Non-capturing groups are used by default. Extending the previous example, you can switch to capturing groups instead.
use grex::RegExpBuilder; let regexp = RegExpBuilder::from(&["big", "BIGGER"]) .with_case_insensitive_matching() .with_capturing_groups() .build(); assert_eq!(regexp, "(?i)^big(ger)?$");
If you find the generated regular expression hard to read, you can enable verbose mode. The expression is then put on multiple lines and indented to make it more pleasant to the eyes.
use grex::RegExpBuilder; use indoc::indoc; let regexp = RegExpBuilder::from(&["a", "b", "bcd"]) .with_verbose_mode() .build(); assert_eq!(regexp, indoc!( r#" (?x) ^ (?: b (?: cd )? | a )
一站式AI创作平台
提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作
AI办公助手,复杂任务高效处理
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!
AI数字人视频创作平台
Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。
AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功 能全面,适合各类写作者提升内容品质和工作效率。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元 石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你 带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景, 提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。
最新AI工具、AI资讯
独家AI资源、AI项目落地
微信扫一扫关注公众号