"I sure am tired of typing extra backslashes in my isearches", I says to myself. "I wonder if anyone has done the unthinkable and implemented perlre in elisp?"


(rxt-pcre-to-elisp "(abc|def)\\w+\\d+")
;; => "\\(\\(?:abc\\|def\\)\\)[_[:alnum:]]+[[:digit:]]+"


PCRE has a complicated syntax and semantics, only some of which can be translated into Elisp. The following subset of PCRE should be correctly parsed and converted:

  • parenthesis grouping ( .. ), including shy matches (?: ... )
  • backreferences (various syntaxes), but only up to 9 per expression
  • alternation |
  • greedy and non-greedy quantifiers *, *?, +, +?, ? and ?? (all of which are the same in Elisp as in PCRE)
  • numerical quantifiers {M,N}
  • beginning/end of string \A, \Z
  • string quoting \Q .. \E
  • word boundaries \b, \B (these are the same in Elisp)
  • single character escapes \a, \c, \e, \f, \n, \r, \t, \x, and \octal digits (but see below about non-ASCII characters)
  • character classes [...] including Posix escapes
  • character classes \d, \D, \h, \H, \s, \S, \v, \V both within character class brackets and outside
  • word and non-word characters \w and \W (Emacs has the same syntax, but its meaning is different)
  • s (single line) and x (extended syntax) flags, in regexp literals, or set within the expression via (?xs-xs) or (?xs-xs: .... ) syntax
  • comments (?# ... )

Most of the more esoteric PCRE features can't really be supported by simple translation to Elisp regexps. These include the different lookaround assertions, conditionals, and the "backtracking control verbs" (* ...)

I am sad to report, however, that I can't get it to work in either xemacs 21.5.28 or emacs 22.1.1. (And MacPorts won't let you install emacs and xemacs simultaneously! How partisan!)

Amusing as it would have been had they tried to translate from one regexp syntax to another using regexps... they did not do that.

Also, my heart grew three sizes when I saw that the first line of the file contains -*- lexical-binding: t -*-

Previously, previously, previously, previously, previously, previously, previously, previously.

Tags: , , , , ,

A Brief History of Generative Art

Previously, previously, previously.

Tags: , , ,

  • Previously