Recursive Regular Expressions

2010-03-24 362 words 2 mins read

<img src="" alt="Yo dawg, I heard you liked regular expressions, so I put a regex in your regex so you can match while you match!" align="bottom" /> The <a class="zem_slink freebase/en/regular_expression" title="Regular expression" rel="wikipedia" href="">regular expressions we use in our daily lives are actually not that “regular.” Most of the languages support some kind of extended regular expressions that are computationally more powerful than the “<a href="">regular” regular expressions as defined by the <a href="">formal language theory.

For instance, the so often used <a href="">capture buffers add <a class="zem_slink freebase/en/data_storage_device" title="Data storage device" rel="wikipedia" href="">auxiliary storage to the regular expressions that allow them to match an arbitrary pattern repeatedly. Or <a href="">look-ahead assertions that allow the regular expression engine to peek ahead before it making a decision.

The <a class="zem_slink freebase/en/perl" title="Perl" rel="homepage" href="">Perl <a class="zem_slink freebase/en/programming_language" title="Programming language" rel="wikipedia" href="">programming language has an especially rich with regex engine. One of the engine’s features is the lazy regular subexpressions. The lazy regular subexpressions are expressed as (??{ <a class="zem_slink freebase/en/computer_programming" title="Computer programming" rel="wikipedia" href="">code }), where the “code” is arbitrary Perl code that gets executed when the moment this subexpression may match.

This allows us to construct something really interesting – we can define a regular expression that has itself in the “code” part. The result is a recursive regular expression!

One of the classical problems that a regular expression can’t match is the language n1n, i.e., a <a class="zem_slink freebase/en/string" title="String (computer science)" rel="wikipedia" href="">string with a number of zeroes followed by an equal number of ones. Surprisingly, using the lazy regular subexpressions this problem becomes tractable!

Here is a Perl regular expression that matches n1n:

my $regex = qr/0(??{$regex})*1/;

This regular expression matches a followed by itself zero or more times, followed by a one. If the itself part doesn’t match, then the string this regular expression matches is 01. If the itself part matches once, the string this regular expression matches is 0011. If it matches two times, the string is 000111, …, etc.

Read More: <a href=""><div class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href=""><img class="zemanta-pixie-img" src="" alt="Reblog this post [with Zemanta]" /><span class="zem-script more-related more-info pretty-attribution paragraph-reblog">


Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it