Hi,
I would like to parse color names in X11's rgb.txt. They look like this:
119 136 153 light slate gray
119 136 153 LightSlateGray
When parsing each line, I would like to get the color triplet, and the
name. I tried it like this:
(let ((color-scanner
(cl-ppcre:create-scanner
"^ +([0-9]{0,2}) +([0-9]{0,2}) +([0-9]{0,2}) +([ a-zA-Z0-9]+)")))
(cl-ppcre:scan-to-strings color-scanner " 1 2 3 foo bar baz "))
This gives #("1" "2" "3" "foo bar baz ") for the substrings. How can
I get rid of the trailing spaces?
Tamas
(message (Hello 'Tamas)
(you :wrote :on '(Sun, 22 Jul 2007 10:42:30 +0200))
(
TP> (let ((color-scanner
TP> (cl-ppcre:create-scanner
TP> "^ +([0-9]{0,2}) +([0-9]{0,2}) +([0-9]{0,2}) +([ a-zA-Z0-9]+)")))
TP> (cl-ppcre:scan-to-strings color-scanner " 1 2 3 foo bar baz "))
TP> This gives #("1" "2" "3" "foo bar baz ") for the substrings. How can
TP> I get rid of the trailing spaces?
via non-greedy repetition +?:
^\s*(\d+)\s+(\d+)\s+(\d+)\s+([\s\w]+?)\s*$
also note that if you use \s instead of " " it would be somewhat more
readable.
)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"scorn")
* Tamas Papp <··············@pu100877.student.princeton.edu> :
| Hi,
|
| I would like to parse color names in X11's rgb.txt. They look like this:
|
| 119 136 153 light slate gray
| 119 136 153 LightSlateGray
|
| When parsing each line, I would like to get the color triplet, and the
| name. I tried it like this:
|
| (let ((color-scanner
| (cl-ppcre:create-scanner
| "^ +([0-9]{0,2}) +([0-9]{0,2}) +([0-9]{0,2}) +([ a-zA-Z0-9]+)")))
| (cl-ppcre:scan-to-strings color-scanner " 1 2 3 foo bar baz "))
|
| This gives #("1" "2" "3" "foo bar baz ") for the substrings. How can
| I get rid of the trailing spaces?
Use the right tool for the job :)
(defun parse-rgb-line (string &key (start 0) end)
(multiple-value-bind (r endr)
(parse-integer string :start start :end end :junk-allowed t)
(multiple-value-bind (g endg)
(parse-integer string :start (1+ endr) :end end :junk-allowed t)
(multiple-value-bind (b endb)
(parse-integer string :start (1+ endg) :end end :junk-allowed t)
(let ((name-startpos
(position-if-not (lambda (c) (case c ((#\Tab #\Space) t)))
string :start (1+ endb))))
(values (format nil "#~2,'0X~2,'0X~2,'0X" r g b)
(subseq string name-startpos end)))))))
--
Some people, when confronted with a problem, think ``I know, I'll use
regular expressions.'' Now they have two problems. --Jamie Zawinski, in
comp.lang.emacs
There is always a problem in making `wise-ass post'. The code I posted
doesn't solve the OPs problem, but I'm not fixing it here...
* Madhu <··············@robolove.meer.net>
| * Tamas Papp <··············@pu100877.student.princeton.edu> :
|
[...]
|| This gives #("1" "2" "3" "foo bar baz ") for the substrings. How can
|| I get rid of the trailing spaces?
|
| Use the right tool for the job :)
[...]
--
Madhu
On Sun, 22 Jul 2007 17:32:42 +0530, <·······@meer.net> wrote:
> * Tamas Papp <··············@pu100877.student.princeton.edu> :
>
>| I would like to parse color names in X11's rgb.txt. They look like this:
>|
>| 119 136 153 light slate gray
>| 119 136 153 LightSlateGray
>
> Use the right tool for the job :)
Indeed.
The file structure is fixed, at least my copy is.
The three integers take the same columns, if they are 0 or 255
i.e. it's fixed width
The name starts at the same location each line.
Comments apparently have a leading '!' or '#'
Seems like a simple task for read-line and subseq.
Maybe a little code to handle different starting colums for colornames
in other copies of 'rgb.txt'.
I suppose it depends how portability factors in, including getting any
packages employed into the distribution.
--
Lisp: Powering `Impossible Thoughts since 1958
--
Posted via a free Usenet account from http://www.teranews.com