awwx.ws

re3

Access to MzScheme regular expressions

http://awwx.ws/re3.arc:

(def re-match-pat (pat str)
  (ac-niltree (scheme.regexp-match (scheme.pregexp pat) str)))

(def re-match (pat str)
  (only.cdr (re-match-pat pat str)))

(def re-split (delim str)
  (scheme.regexp-split (scheme.pregexp delim) str))

(def re-replace (pat text replacement)
  (scheme.regexp-replace* (scheme.pregexp pat) text replacement))

See the PLT Scheme regexp chapter for documentation on the Perl-style regular expression syntax provided by these functions.

Examples

re-match-pat

arc> (re-match-pat "def" "abcdefghi")
("def")
arc> (re-match-pat "xyz" "abcdefghi")
nil
arc> (re-match-pat "(d)(ef)" "abcdefghi")
("def" "d" "ef")

re-match

arc> (re-match "xyz" "abcdefghi")
nil
arc> (re-match "def" "abcdefghi")
nil
arc> (re-match "(def)" "abcdefghi")
("def")
arc> (re-match "(d)(ef)" "abcdefghi")
("d" "ef")

re-split

arc> (re-split "," "abc,def,ghi")
("abc" "def" "ghi")

re-replace

arc> (re-replace "\\d" "abc1def2ghi3" "D")
"abcDdefDghiD"

Acknowledgments

I thought I vaguely remembered deriving this from some regexp code in Anarki, but I couldn’t find it, so perhaps it was some other code that I borrowed from Anarki that I forgot about.

Prerequisites

This hack depends on arc3.1 and scheme0.

License

Same as Arc.

Contact me

Twitter: awwx
Email: andrew.wilcox [at] gmail.com