In The SRE regular-expression notation, Olin Shivers describes a "100% solution" for working with regular expressions in Scheme. Unfortunately, his solution has only been implemented in SCSH until now, while other Schemes had various "80% solutions" (or worse...) By releasing scsh-regexp, I aim to change that.
There are two parts to Olin's solution: the SRE notation and the regexp API. The SRE notation is a Scheme representation of POSIX regular expression strings; for example, (: (| upper ("aeiou") digit) (* digit)) is equivalent to "[A-Zaeiou0-9][0-9]*". See Summary SRE syntax in the SCSH manual. In essence, the SRE notation is syntactic sugar.
The SCSH regexp API provides a standardised way to search strings for regular expressions, manipulate matches, and operate replacements. Here's a (trivial) example of listing dates found in a text document:
regexp "([0-9][0-9]?)/([0-9][0-9]?)/([0-9]+)")) (define (process-match m) (let-match m (whole m d y) (for-each display (list whole " = month:" m " day:" d " year:" y "\n")))) (define text "Nothing happened on 04/01/2002 or 04/01/2003") (regexp-for-each date-re process-match text)]]>
It turns out that the SCSH regexp API can be implemented in terms of the basic primitives offered by various Schemes. As a start, I have ported the regexp API to Chicken and SISC. SISC was the more interesting case, as it has no regexp support, but offers access to the underlying Java VM.
wget http://www.call-with-current-continuation.org/eggs/scsh-regexp.egg tar xvfz scsh-regexp.egg scsh-regexpTo load it, cd into the extracted directory, start SISC and type
(require-extension (lib scsh-regexp/scsh-regexp))
Should the Chicken repository be unavailable, you can also download scsh-regexp from Google Code.
So what's left to do?
I'm unlikely to work on these (or other) enhancements unless I hear from users — so, again, you are encouraged to get in touch.