Tools for generating regular expressions
RegexGenerator
In looking for libraries that work similarly to the other tools discussed in this article, my research turned up limited results. For Python, I found RegexGenerator [16] and its associated regex-generator-lib [17].
Listing 1 shows a short script for RegexGenerator, which determines a regular expression for the string 415-5553-7676
. You can then save and run the regular expression as rg.py
(Listing 2). The generated regex shown in Listing 2 does the trick: a pattern consisting of three digits, followed by a minus sign, four digits, another minus sign, and another four digits.
Listing 1
RegexGenerator Script
from RegexGenerator import RegexGenerator myRegexGenerator = RegexGenerator("415-5553-7676") print(myRegexGenerator.get_regex())
Listing 2
rg.py
$ python3 rg.py \d{3}[-]\d{4}[-]\d{4}
However, this generated regex lacks precision. Not only does it match the above string, but it also matches other strings, such as 123-4567-8901
or foo-123-5553-7676-bar
. According to this regular expression, the digits can be arbitrary and the pattern can include other characters because the regex does not use delimiters such as \b
for word boundary, ^
for beginning of line, and $
for end of line.
Instead, A regex of ^415\-5553\-7676$
would be more precise and easier to read, resulting in the three digits 415
followed by a minus sign, three times the number 5
followed by a 3
, another minus sign, and then two times the sequence of digits 76
including characters for the beginning of the line (^
) and the end of the line ($
).
rex
If you use the rex
tool from Python's Test-Driven Data Analysis (tdda) package [18], there is a very neat, practical use case. The example shown in Listing 3 determines the regular expression for naming image files. All of the file names start with the three letters DSC
, followed by five numbers, a period, and the three letters .JPG
.
Listing 3
rex from tdda
$ ls images/*.JPG DSC06743.JPG DSC06745.JPG DSC06751.JPG DSC06754.JPG $ ls images/*.JPG | python3 tdda/rexpy/rexpy.py ^DSC\d{5}\.JPG$
The regular expression is correct, as far as it goes; it also includes two additional delimiters, ^
for beginning of line and $
for end of line. The regex excludes false positives as long as the pattern consists of the three uppercase letters DSC
followed by any five sections and the strings .JPG
. However, a more precise regular expression would be DSC067((4[35])|(5[14]))\.JPG
.
Again, the results returned by rex
from the tdda library end up in the middle of the pack in terms of precision. While rex
successfully matches the DSC
and JPG
portions of the pattern, it can produce a false positive for the digits in the sequence.
RegExTractor
To demonstrate the capabilities of RegExTractor [19], another Python regex extractor, I used random German license plates. Listing 4 first outputs the license plates followed by the regular expression that matches all license plates.
Listing 4
Running RegExTractor
$ python main.py Kennzeichen: A-BC 1234 CB-LN 5246E FR-CG 1554 TUT-R 712 AA-LN 5E The regex for this: [A-Z][A-Z]{0,2}\-[A-Z][A-Z]?\ [0-9][0-9A-Z]{1,4}
However, the regular expression that RegExTractor outputs is not correct. In particular, the last partial pattern of [0-9][0-9A-Z]{1,4}
allows any digit followed by a combination of one to four capital letters and digits.
A more accurate solution would be the subexpression [0-9]{1,4}E+
for one to four digits, followed only by the capital letter E
for electric cars. In testing, this problem occurred repeatedly. Consequently, I cannot recommend RegExTractor.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.