Monday, February 13, 2012

pattern matching with regexps

This is just a reminder to myself to think about extending pattern matching to regular expressions on strings.  It'd be a great feature, though with the famous caveat about regular expressions.  [I have a problem.  I think I can solve it with regular expressions.  Now I have two problems.]



(define sample
  "(?P<x>[0-9]+)+(?P<y>[0-9]+)" -> (+ (string->int x) (string->int y))
  "[^x]+(?P<nx>x+)[^x]+" -> (string-length nx)
  )


I still remember looking through the ejabberd source code, trying to find their xml parser.  When it finally dawned on me that it was automagically hidden in the pattern matching, I was impressed.

1 comment:

  1. Generalizing a bit, how about another syntax for pattern matching on 'regular languages':

    (regmatch x with
    (reg:concat (reg:+ (reg:class (class:whitespace)))
    [xs (reg:* (reg:class (class:char #\x)))]
    (reg:+ (reg:class (class:whitespace)))
    )
    -> xs)

    Where [var exp] binds that sub-expression to a variable...
    [sadly I can't seem to wrap that with pre so it's probably unreadable]

    Also, need to see what facilites erlang might have that are similar.

    ReplyDelete