Regular Expressions/Perl-Compatible Regular Expressions

Perl has a richer and more predictable syntax than even the ../POSIX Extended Regular Expressions/ syntax. An example of its predictability is that \ always quotes a non-alphanumeric character. An example of something that is possible to specify with Perl but not POSIX is whether part of the match wanted to be greedy or not. For instance in the pattern /a.*b/, the .* will match as much as it can, while in the pattern /a.*?b/, .*? will match as little. So given the string "a bad dab", the first pattern will match the whole string, and the second will only match "a b".

For these reasons, many other utilities and applications have adopted syntaxes that look a lot like Perl's. For example, Java, Ruby, Python, PHP, exim, BBEdit, and even Microsoft's .NET Framework all use regular expression syntax similar to that used in perl. Not all "Perl-compatible" regular expression implementations are identical, and many implement only a subset of Perl's features.

Examples
Conventions used in the examples: The character 'm' is not always required to specify a perl match operation. For example, m/[^abc]/ could also be rendered as /[^abc]/. The 'm' is only necessary if the user wishes to specify a match operation without using a forward-slash as the regex delimiter. Sometimes it is useful to specify an alternate regex delimiter in order to avoid "delimiter collision". See 'perldoc perlre' for more details.

metacharacter(s) ;; the metacharacters column specifies the regex syntax being demonstrated =~ m//          ;; indicates a regex match operation in perl =~ s///         ;; indicates a regex substitution operation in perl

In the table heading below, "M-c" stands for "Metacharacters".

Use in Tools
Tools and languages that utilize Perl regular expression syntax include:


 * Java
 * Perl
 * Python
 * PHP
 * PHP

Links

 * Perl regular expressions at perl.org
 * Perl Compatible Regular Expressions library at pcre.org
 * Perl Regular Expression Syntax at boost.org
 * Regular_expressions