# Minimizing the length of regular expressions, in practice

DevFeed: [Minimizing the length of regular expressions, in practice](<https://devfeed.tech/articles/minimizing-the-length-of-regular-expressions-in-practice-27405.md>)

Original publisher: [Read original article](<http://engineering.khanacademy.org/posts/shortest-regex.htm>)

Author: Khan Academy

Published: 2016-05-23T22:00:00Z

Content type: article

Language: en

Sources: [Khan Academy](<https://devfeed.tech/sources/khan-academy.md>)

Topics: [Regular expression](<https://devfeed.tech/topics/regular-expression.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [computational complexity](<https://devfeed.tech/topics/computational-complexity.md>), [Software Engineering](<https://devfeed.tech/topics/software-engineering.md>)

Tags: [algorithms](<https://devfeed.tech/tags/algorithms.md>), [appengine](<https://devfeed.tech/tags/appengine.md>), [deterministic-finite-automata](<https://devfeed.tech/tags/deterministic-finite-automata.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [infrastructure](<https://devfeed.tech/tags/infrastructure.md>), [interviews](<https://devfeed.tech/tags/interviews.md>), [news](<https://devfeed.tech/tags/news.md>), [questions](<https://devfeed.tech/tags/questions.md>), [regular-expressions](<https://devfeed.tech/tags/regular-expressions.md>)

## AI overview

To fit a CDN's 512-character limit while routing Khan Academy static URLs separately from dynamic ones, the author reduced a 402,025-character regex listing every file to a 294-character pattern. Combining shared prefixes cut a config-derived pattern to 824 characters, but the key step was recognizing that the regex only needed to distinguish known static paths from dynamic routes; it could match invalid URLs too, since either provider could return a 404. The shortest resulting prefixes risked misrouting future dynamic routes, so the author extended them to end at a slash. The final pattern was readable and within the limit, with a small robustness tradeoff against future route changes.

## Source excerpt

By Craig Silverstein The problem Software engineering interviews tend to be full of "algorithms" questions, because they're easy ... Read more