Can I get the result of a match?

I've been on a bit of a tear with match lately but I find myself re-running the match operation to take advantage of the match. For instance:

define (find-email candidate)
  (define rule-name "Email Address")
  (define simple-email-regex (pregexp "\\S+@\\S+\\.\\S+"))
  (match candidate
    [(pregexp simple-email-regex)
     (list (regexp-match simple-email-regex candidate)) #t)]
    [_ (list null #f)]))

I've simplified the code a little bit but I want to pass back what matched and some metadata (in this case simply a boolean).

I'm looking to tidy up my code and so I wonder if a fellow match user could tell me where match stores the result of the match. So I don't rerun the rexexp match?

The documentation of pregexp in match indicates that you can put a match pattern after the regexp pattern. In your case, you probably want:

(define (find-email candidate)
  (match candidate
    [(pregexp #px"\\S+@\\S+\\.\\S+" match-result)
     (list match-result #t)]
    [_ (list null #f)]))

4 Likes

Thanks, I missed that.