From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Lee Sau Dan Newsgroups: gmane.emacs.help Subject: Re: a function for string splitting Date: 26 Nov 2002 15:49:00 +0100 Organization: Rechenzentrum der Universitaet Freiburg, Germany Sender: help-gnu-emacs-admin@gnu.org Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=cn-big5 Content-Transfer-Encoding: 8bit X-Trace: main.gmane.org 1038325562 8877 80.91.224.249 (26 Nov 2002 15:46:02 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Tue, 26 Nov 2002 15:46:02 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18GhuO-0002IL-00 for ; Tue, 26 Nov 2002 16:45:56 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10) id 18GhvL-0000FN-00; Tue, 26 Nov 2002 10:46:55 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!logbridge.uoregon.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.arcor-online.net!news.belwue.de!news.uni-freiburg.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 65 Original-NNTP-Posting-Host: camaro.informatik.uni-freiburg.de User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 Original-Xref: shelby.stanford.edu gnu.emacs.help:107486 Original-To: help-gnu-emacs@gnu.org Errors-To: help-gnu-emacs-admin@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.help:4037 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:4037 >>>>> "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, ...). Try: (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 regex-fragment-2 regex-fragment-3 regex-fragment-4) date-string) (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 necessary. -- Lee Sau Dan §õ¦u´°(Big5) ~{@nJX6X~}(HZ) E-mail: danlee@informatik.uni-freiburg.de Home page: http://www.informatik.uni-freiburg.de/~danlee