A composable regex repository
Debuggex has launched a regex repository! Starting today, this is how you can match an IPv4 address on our site:
(?&.ipv4)
(The (?&
syntax is borrowed from PCRE's
regex subroutines.)
The concept is really simple - Debuggex has a list of pre-built expressions that you can use instead of wasting your time figuring them out by yourself. We've already got a list of almost a hundred expressions, and we are constantly adding more.
There are a few key features that make our implementation significantly better than what you may have seen in other places.
Unit Tests
Debuggex was designed from the start with unit-testing and backwards compatibility in mind. Each regex in the repository has a suite of tests that make it bulletproof.
The unit tests also serve as working examples for how to use a regex. Ultimately, you spend less time fiddling around, and more time focusing on the things that matter to you.
Composability
Regular expressions tend to be at a very low abstraction level. If you want to find something in a log file, you should be thinking in terms of ip addresses and urls - not dots, digits, and slashes. Working at such a low level quickly gets very complicated.
Composability makes abstraction way easier. Suppose you have a log file with entries consisting of an IP address (either IPv4 or IPv6) followed by a quoted referrer url. Here's how you would match that:
((?&.ipv4)|(?&.ipv6)) (?&.quotStr)
That's it! Arbitrarily complex expressions are hidden behind a simple variable name. You can use these just like any other piece of a regex. To turn this into an expression that your language will understand, just click "Code Snippet". Debuggex will compile the expression and give you:
var regex = new RegExp("((?:(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2}))\\.){3,3}(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2})))|(?:(?:(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){7,7}(?:(?:[0-9A-Fa-f]){0,4}))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){6,6}:(?:(?:[0-9A-Fa-f]){0,4}))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){5,5}:(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,1}(?:(?:[0-9A-Fa-f]){0,4}))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){4,4}:(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,2}(?:(?:[0-9A-Fa-f]){0,4}))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){3,3}:(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,3}(?:(?:[0-9A-Fa-f]){0,4}))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){2,2}:(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,4}(?:(?:[0-9A-Fa-f]){0,4}))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){6,6}(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2}))\\.){3,3}(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2})))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,5}:(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2}))\\.){3,3}(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2})))|(?:::(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,5}(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2}))\\.){3,3}(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2,2})|(?:[0-9]{1,2})))|(?:(?:(?:[0-9A-Fa-f]){0,4})::(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,5}(?:(?:[0-9A-Fa-f]){0,4}))|(?:::(?:(?:(?:[0-9A-Fa-f]){0,4}):){0,6}(?:(?:[0-9A-Fa-f]){0,4}))|(?:(?:(?:(?:[0-9A-Fa-f]){0,4}):){1,7}:))))\\ (?:\"((?:\\\\.|[^\"\\\\]){0,})\")", "g");
How long would it have taken to come up with that expression yourself? Keep in mind that the IP is ensured to be correctly formatted, and any escaped quotes in the referrer url will be correctly matched.
Conclusion
We think you'll find this repository incredibly useful, and hope it'll save you a ton of time.
We love feedback! If you have some, get in touch.
p.s. If you'd like to compose using your own named expressions, that will be launching very soon!