Common Lisp - String parsing 
Author Message
 Common Lisp - String parsing

Hello,

I want to parse strings like "hello world" into a list containing all the
words: ("hello" "world").
How could I do this?

Thanks in advance.

Gruss
Julian

--
Zum Antworten via Mail .nospam in der ElektroPost (eMail fuer die Neudeutschen)
Adresse entfernen.
(To reply simply remove .nospam in my email address.)



Fri, 19 Sep 2003 03:51:55 GMT  
 Common Lisp - String parsing

Quote:

> I want to parse strings like "hello world" into a list containing all the
> words: ("hello" "world").
> How could I do this?

;;From CLOCC's string.lisp
(defun split-seq (seq pred &key (start 0) end key strict)
  "Return a list of subseq's of SEQ, split on predicate PRED.
Start from START, end with END.  If STRICT is non-nil, collect
zero-length subsequences too.
  (split-seq SEQ PRED &key (start 0) end key strict)"
  (declare (sequence seq) (type (function (t t) t) pred) (fixnum start))
  (loop :for st0 = (if strict start
                       (position-if-not pred seq :start start
                                        :end end :key key))
        :then (if strict (if st1 (1+ st1))
                  (position-if-not pred seq :start (or st1 st0)
                                   :end end :key key))
        :with st1 = 0 :while (and st0 st1) :do
        (setq st1 (position-if pred seq :start st0 :end end :key key))
        :collect (subseq seq st0 st1)))

(defun split-string (str chars &rest opts)
  "Split the string on chars."
  (declare (string str) (sequence chars))
  (apply #'split-seq str (lambda (ch) (declare (character ch)) (find ch chars))
         opts))

;;Simpler version using loop
(defun split-string-2 (string &optional (separator #\Space))
  (loop for i = 0 then (1+ j)
        as j = (position separator string :start i)
        collect (subseq string i j) while j))

;;Same as above, but with do* instead of loop
(defun split-string-3 (string &optional (separator #\Space))
  (do* ((i 0 (1+ j))
        (j (position separator string :start i) (position separator string :start i))
        (words (list (subseq string i j)) (push (subseq string i j) words)))
      ((null j) (nreverse words))))

CL-USER(11): (split-string "hello world" '(#\Space))
("hello" "world")
CL-USER(12): (split-string-2 "hello world")
("hello" "world")
CL-USER(13): (split-string-3 "hello world")
("hello" "world")



Fri, 19 Sep 2003 14:31:11 GMT  
 Common Lisp - String parsing

Thanks.

Gruss
Julian

--
Zum Antworten via Mail .nospam in der ElektroPost (eMail fuer die Neudeutschen)
Adresse entfernen.
(To reply simply remove .nospam in my email address.)



Fri, 19 Sep 2003 18:22:43 GMT  
 Common Lisp - String parsing
This works in ACL and PowerLisp:

(defun string-parse
    (string &optional (token #\space))
  "Parse a string into a list of strings."
  (labels ((cons-list (p str list-out)
             (if p (cons (subseq str 0 p) list-out) list-out)))
    (do* ((str      string                (subseq str (1+ p)))
          (p        (position token str)  (position token str))
          (list-out (cons-list p str nil) (cons-list p str list-out)))
        ((null p) (nreverse (cons str list-out))))))

slong

Quote:

> Thanks.

> Gruss
> Julian

> --
> Zum Antworten via Mail .nospam in der ElektroPost (eMail fuer die Neudeutschen)
> Adresse entfernen.
> (To reply simply remove .nospam in my email address.)



Sun, 21 Sep 2003 11:03:05 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Interesting read: Pragmatic Parsing in Common Lisp

2. What I want from my Common Lisp vendor and the Common Lisp community

3. lucid common lisp -- C -- Common lisp intercallability

4. Lucid (Sun) Common Lisp vs Allegro (Franz) Common Lisp - the Summary

5. Lucid (Sun) Common Lisp vs Allegro (Franz) Common Lisp

6. Sun Common Lisp vs. Allegro Common Lisp

7. Lisp editor that can parse strings

8. Parsing strings into LISP syntax

9. Concatenate strings in common lisp.

10. Common Lisp: String to Number conversion

11. New Common Lisp, Lisp-to-C translation, Lisp library for C

12. #+COMMON, #+COMMON-LISP, #+CLtL1 or #+CLtL2?

 

 
Powered by phpBB® Forum Software