Using rxmatch() and rxsub() with PCRE regex

Introduction

This article shows how to use the string.rxmatch() and string.rxsub() functions with PCRE regex, these functions are the equivalent of string.gmatch() and string.gsub(), but using PCRE regex instead of the simpler Lua pattern matching. There are also some (hopefully) helpful PCRE regex examples, including some useful features (that are not available with Lua pattern matching).

For information on the differences between PCRE regex and Perl and POSIX regex, see these PCRE reference links: Perl Differences and Differences from POSIX regex.

All regex patterns used in this article are listed on the PCRE Samples page, including links to any examples on the rxmatch() and rxsub() pages.

To find out more you can follow the More Information links below or do a quick web search (“PCRE”, “PCRE regex”, “PCRE tutorial”).

Tip: I found the Regex 101 online tester very helpful when creating/testing the expressions on this page. The Regex Buddy application (Windows only) also comes highly recommended, though it is not free. And you can google to find other regex testers.

Leave A Comment?