[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a function for string splitting

From: Lee Sau Dan
Subject: Re: a function for string splitting
Date: 26 Nov 2002 15:49:00 +0100
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

>>>>> "Luis" == Luis O Silva <> writes:

    Luis> Dear Emacs community, I'm writing a function for translating
    Luis> dates in the form of a string into Spanish and Russian. For
    Luis> example, you have:

    Luis> "Thu, 21 Nov 2002 16:05:50 -0600 (CST)"

    Luis> Within my function I used a `let' expression of the form:

    Luis> (let ((day (substring "Thu, 21 Nov 2002 16:05:50 -0600
    Luis> (CST)" 0 3)) (month (substring "Thu, 21 Nov 2002 16:05:50
    Luis> -0600 (CST)" 8 11))) ...)

    Luis> All works fine provided that there isn't any date with
    Luis> one-digit day, i. e., "Fri, 8 Nov 2002 11:56:37 -0500 (CST)"

    Luis> My question is what function I could use for correctly
    Luis> splitting the string.

    Luis> Please be indulgent with me since

    Luis> 1. I'm not a programmer 2. I don't have access to the elisp
    Luis> manual even on-line (my connection is very slow, only
    Luis> sufficient to download my e-mail).

Consider learning "regular expressions",  which are not only useful in
Emacs,  but also useful  throughout the  unix environment  (e.g. grep,
sed, awk, vi, ...).


(let ((date-string "Thu, 21 Nov 2002 16:05:50 -0600")
      (regex-fragment-1 "\\([A-Z][a-z]*\\), ")
      (regex-fragment-2 "\\([0-9]+\\) \\([A-Z][a-z]*\\) \\([0-9]+\\) ")
      (regex-fragment-3 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) ")
      (regex-fragment-4 "\\([+-][0-9]*\\)" ))
  (if (string-match (concat regex-fragment-1
    (let ((day-of-week (match-string 1 date-string))
          (day (match-string 2 date-string))
          (month-name (match-string 3 date-string))
          (year (match-string 4 date-string))
          (hour (match-string 5 date-string))
          (minute (match-string 6 date-string))
          (second (match-string 7 date-string))
          (tz-spec (match-string 8 date-string)))

    (list day month-name year hour minute second tz-spec))))

Note that I've broken up the  regex into 4 fragments to make it usenet
friendly,  rejoining  them with  (concat  ...).   This  is not  really

Lee Sau Dan                     李守敦(Big5)                    ~{@nJX6X~}(HZ) 

Home page:

reply via email to

[Prev in Thread] Current Thread [Next in Thread]