[racket] Question about string conversion

From: m0nastic at tengulabs.com (m0nastic at tengulabs.com)
Date: Thu Jul 18 13:19:41 EDT 2013

Thanks. I really appreciate all the responses I've gotten about this,
you folks are all Aces.

I'll give net/head another look.  It was actually the first thing I
looked into, but seemed like it was mostly for dealing with email
addresses (which counter-intuitively aren't the data fields in the
header that I'm concerned with).

I'm actually dealing with the series of "Received from xxx by yyy"
fields that get added at each hop through the delivery chain by the SMTP
servers.

I ended up with three separate regular expressions (one specifically for
how Yahoo mail servers report that information), which I can probably
combine at some point for simplicity.

The match examples you listed are definitely something I should
experiment with. They actually resemble pattern matching in Haskell if I
squint.

Thanks again,

-Chris





________________________________________
From: Greg Hendershott <greghendershott at gmail.com>
Sent: Thursday, July 18, 2013 10:01 AM
To: m0nastic at tengulabs.com
Cc: users at racket-lang.org
Subject: Re: [racket] Question about string conversion




To add to what Carl and Robby said:




On Thu, Jul 18, 2013 at 2:29 AM,  <m0nastic at tengulabs.com> wrote:
> (although truth be told, mail headers are surprisingly nonstandard even
> within a single message)




That's where net/head could definitely help. (Especially for SMTP
headers, which tend to be more "interesting" than typical HTTP
headers.)




> I eventually discovered that I could sort of cheat, by just wrapping the
> regexp-match function with a car (which worked, because this particular
> list only had one element), and then it was usable from then on (and
> validated true from "string?").




It's worth walking through what Carl described. Actually you're lucky,
you have a specific example of something you want to do, which touches
on a few basic aspects of Racket.




Having said that, one pattern I've settled into using with regexps is
to use `match`, which makes it convenient to "de-structure" the list.
For example:




(match "From: Me"
  [(pregexp "^(.+):\\s*(.+)$" (list all key val))
   (displayln all)   ; From: Me
   (displayln key)   ; From
   (displayln val)]) ; Me




If you don't need the "all" part, you can supply _ like so:




(match "From: Me"
  [(pregexp "^(.+):\\s*(.+)$" (list _ key val))
   (displayln key)
   (displayln val)])




Which is the pattern I use a lot.




Again, you probably want to use net/head to deal with the gory details
of SMTP headers as Robby suggested, and it would definitely be good to
walk through the explorations Carl described.


Posted on the users mailing list.