> On Jul 22, 2017, at 12:30 PM, David Storrs <[email protected]> wrote:
> 
> One thing that would solve a lot of this issue would be if the pregexp syntax 
> added support for named captures as in Perl and the PCRE library that exports 
> them. 
> 
> Alternatively, if 'match' made the results of a successful regexp test 
> available to the bodies on the RHS then you could do the same thing by 
> accessing the result list.  Perhaps if match would allow the RHS to be a 
> function?



This could also be a light syntactic abstraction over `cond`:

#lang at-exp racket

(define-syntax (regexp-case stx)
  (syntax-case stx (=> else)
    [(_ STR [PAT => PROC] ... [else . ELSE-BODY])
     #'(cond
         [(regexp-match PAT STR) => PROC] ...
         [else . ELSE-BODY])]
    [(_ STR [PAT => PROC] ...) #'(regexp-case STR [PAT => PROC] ... [else 
#f])]))

(define str "[04, foo, 03.5]")
(define pat @(pregexp @~a{\[(\d+),\s*(\w+),\s*([.\d]+)}))
(regexp-case str
             [pat => (λ (res) (match-let ([(list _ item-num name price) res])
                                (println (~a "item name: " name ", number: " 
item-num ", price: $" price))))])
                                
(regexp-case "foobar"
             [#px"oo.+(.)" => (lambda (res) (println (~a "Regexp match results 
were: " res)))])

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to