Creating more readable regular expressions with Simple Regex Language
What Next?
You might be wondering what you can do with the finished SRL expression, since Grep and most other tools only digest conventional regular expressions.
If you just need a regular expression on the fly, you can always enter the SRL expression into the test at the SRL website (as described earlier in this article), and then copy the resulting regular expression to Grep or another tool.
Also, some languages have already begun to implement SRL support. You will find special SRL libraries for JavaScript, PHP, Python, and C++ at GitHub under the MIT license [3]. Java and C# libraries are still pending.
The functions and classes of these SRL libraries accept an SRL expression, evaluate it, and convert it into a regular expression. In the case of PHP, the user just has to create an SRL
object and check the text using the method isMatching()
:
$srl = new SRL('one of "eE" literally "rror"');$srl?isMatching('Error'); // is True
In addition to featured keywords like literally
, SRL also offers the keywords shown in Tables 1 to 6. You'll find a detailed reference and many other examples on the official SRL homepage [1].
Table 1
Character Strings
Keyword | Description |
---|---|
literally "string" |
Representative of the character string |
one of "abc" |
One of the characters a, b, or c. |
letter from a to d |
One of the characters a, b, c, or d. |
uppercase letter |
Any uppercase letter. |
any character |
Uppercase or lowercase letter from A to Z, a number from 0 to 9, or an underscore ( |
no character |
All other (special) characters. |
digit from 1 to 4 |
One of the digits 1, 2, 3, or 4, whereby |
anything |
Any character with the exception of a line break. |
new line |
Line break. |
whitespace |
A whitespace character (this includes the space character, the tabulator, and the line break). |
no whitespace |
Character that is not a whitespace character. |
tab |
Tabulator. |
backslash |
Backslash character ( |
raw "[a-z]" |
Stands for the result of the regular expression |
Table 2
Quantifiers
Keyword | Description |
---|---|
exactly 4 times |
Something repeats exactly four times. The expression |
between 2 and 4 times |
Something repeats between two and four times; the following keyword |
optional |
Something may occur, but does not have to. |
once or more |
Something must occur at least once. |
never or more |
Something must occur multiple times or not at all. |
at least 2 times |
Something must occur at least twice. |
Table 3
Groups
Keyword | Description |
---|---|
capture ( |
|
any of ( |
Each |
capture ( |
Captures the expression |
Table 4
Lookarounds
Keyword | Description |
---|---|
if followed by |
Checks whether something particular follows (lookahead). |
if not followed by |
Check whether something does not follow. |
if already had |
Checks whether something was preceding (lookbehind). |
if not already had |
Checks whether something was not preceding. |
Table 5
Flags
Keyword | Description |
---|---|
case insensitive |
Uppercase and lowercase are not of any importance. |
multi line |
The text to be checked runs over multiple lines. |
all lazy |
The evaluation is performed according to the Lazy principle. |
Table 6
Anchors
Keyword | Description |
---|---|
start with |
Something explicitly refers to the start of a string. |
must end |
Something refers to the end of a string. |
Conclusions
SRL is built on the philosophy that, if regular expressions are easier to read, errors will stand out more quickly. It is worth noting, however, that SRL expressions are also complex and difficult to understand if you aren't accustomed to the syntax. SRL does not currently feature comments, which would help to add clarity, and recursion is also missing. Classic Unix text tools such as Grep do not yet provide SRL support; however, you can convert the expression at the SRL website or use an SRL library with some programming languages.
Despite the problems, SRL is still worth a look. If you only use regular expressions occasionally, or if you are using them for the first time, you will get to your destination much faster with SRL. Even regex old-timers might find they can make their expressions more legible with SRL.
In the future, developer Karim Geiger wants to add support for additional programming languages and also standardize the SRL language to define the syntax and commands more clearly. In the long run, he imagines a kind of compiler that translates regular expressions into SRL. He has stated that a Bash version is not planned but is conceivable.
SRL commands are based on English. Geiger has resisted the suggestion to translate the natural language commands to other written languages. He fears that language proliferation would introduce a complexity that could lead to incompatible versions.
Infos
- Simple Regex Language: https://simple-regex.com
- Build Tool for the SRL: https://simple-regex.com/build
- SRL Libraries: https://github.com/SimpleRegex
« Previous 1 2
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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.