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 sub­rou­tines.)

The concept is really simple - Debuggex has a list of pre-built ex­pres­sions that you can use instead of wasting your time figuring them out by yourself. We've already got a list of almost a hundred ex­pres­sions, and we are constantly adding more.

There are a few key features that make our im­ple­men­ta­tion sig­nif­i­cant­ly better than what you may have seen in other places.

Unit Tests

Debuggex was designed from the start with unit-testing and backwards com­pat­i­bil­i­ty in mind. Each regex in the repository has a suite of tests that make it bul­let­proof.

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.

Com­pos­abil­i­ty

Regular ex­pres­sions tend to be at a very low ab­strac­tion 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 com­pli­cat­ed.

Com­pos­abil­i­ty makes ab­strac­tion 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! Ar­bi­trar­i­ly complex ex­pres­sions 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");

(live demo)

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 ex­pres­sions, that will be launching very soon!

Sneak Preview of PCRE Support in Debuggex » « A true regex tester