I’ve struggled with regular expressions in the past and have just kept updating my re until it did what I wanted. These days I would probably use AI. However I came across https://regex101.com tonight which I thought was pretty useful.
Excellent recommendation! I’ve used that site in the past on the very rare occasions when I have to build or modify some regex. I also struggle with it as it’s one of those things that I probably don’t do often enough to keep proficient at.
Not directly related, but CyberChef is a brilliant companion (and self-hostable). The regex section is nowhere as fully featured as regex101, but it has a multitude of different encoder/decoder/hash/conversion utilities which I often turn to.
There are different dialects too aren’t there? I think I’ve seen some perl specific mentions here and there.
This is regex101.com’s guilde for choosing your “poison” and so I think it’s obligatory to mention this.
Selecting flavor
Picking a suitable flavor can be challenging, considering how many options there are depending on the language you are using.
Below is a simple cheat sheet to help you pick a suitable flavor for your language/tool, based on list of supported languages on regex101.
Important information
The PCRE libraries are compiled in 16-bit mode to be compatible with your browser and Javascript.
All flavors run natively in your browser!
You can read more here.
Reference table
| Language | RegEx101 Support | Comments |
|---|---|---|
| JavaScript | Full | Uses your browsers native implementation |
| PHP | Full | Recent versions of PHP use PCRE2. |
| Perl | Partial | Use the PCRE2 flavor for the greatest support |
| Python | Emulated | Emulated through PCRE but provides a good baseline |
| Ruby | Partial | Oniguruma and Onigmo are quite similar to PCRE in their feature set |
| Java | Full | Newer versions of Java have greater support for variable width lookbehinds |
| C++ | Full | Use the EcmaScript (JavaScript) flavor |
| Golang | Full | Uses googles RE2 engine |
| .NET | Full | Uses .NET 7 |
| Rust | Full | Uses the Regex Crate |
| Scala | Full | Use the Java flavor |
Note: This is a living document and subject to change. Feel free to suggest improvements here.
I have never met a single REGEX expert in my life. Everyone I know has problems with it, so in the end I decided that REGEX is some kind of huge joke played on Mankind.
I tried to design SVD2DB
with REGEX but failed. Note current (Gema) version of my application is
https://sourceforge.net/projects/mecrisp-stellaris-folkdoc/files/svd2db-v2.zip
When I tried Gema
I found that it was actually easy after the steep Gema earning exercise.
Sure any, automatic text replacement that spans lines is going to be complex and have all kinds of possible side effects, but at least Gema is understandable by a humble (non Mensa) electronics tech, unlike REGEX.
So I say, give up the REGEX pipe dream, take up SED, or Gema, or something that works. We all want the quick dream solution to our search and replace problems like REGEX, but it’s more like a nightmare in reality.
Cheers,
Terry