I'd like to perform a regexp search, capture part of a matched string, and save
that part to a list. (Eventually the plan is to save this list to a file in a
particular format, but for now a simple list will do.) I've tried the
following, but it doesn't seem to do anything when I M-x eval-region
over the
whole document and M-x first-words-in-numbered-list
.
(defun first-words-in-numbered-list ()
"gets the first word in lines that start with numbered list and builds those
words into a list, e.g. running the function on the following text:
foo
bar
1. alpha zulu
2. bravo tango
quux schmucks
3. charlie foxtrot
10. kilo romeo
should produce a list (\"kilo\" \"charlie\" \"bravo\" \"alpha\")"
(interactive)
(goto-char 1)
(setq first-words '())
(while (re-search-forward
"^[[:space:]]*[[:digit:]]+[.)]?[[:space:]]+\([[:word:]]\)"
nil t)
;; for debugging
(message (match-string 1))
(sleep-for 2)
;; for actually storing the list
(setq first-words (cons (match-string 1) first-words))))
(defun print-list-elements (list)
"Print each element of LIST on a line of its own."
(while list
(print (car list))
(setq list (cdr list))))
(print-list-elements first-words)
foo
bar
1. alpha
2. bravo
quux
3. charlie
10. kilo
Edit: I've updated the file to look like this:
(defun first-words-in-numbered-list ()
"gets the first word in lines that start with numbered list and builds those
words into a list, e.g. running the function on the following text:
foo
bar
1. alpha zulu
2. bravo tango
quux schmucks
3. charlie foxtrot
10. kilo romeo
should produce a list (\"kilo\" \"charlie\" \"bravo\" \"alpha\")"
(interactive)
(goto-char 1)
(setq first-words '())
(while (re-search-forward
"^[[:digit:]]+[.)]?[[:space:]]+\\([[:word:]]+\\)"
nil t)
(setq first-words (cons (match-string 1) first-words))))
foo
bar
1. alpha
2. bravo
quux
3. charlie
10. kilo
which is most of the way there! The problem is that when I C-h v first-words <RET>
, I get the following:
first-words's value is (#("kilo" 0 4
(fontified t))
#("charlie" 0 7
(fontified t))
#("bravo" 0 5
(fontified t))
#("alpha" 0 5
(fontified t)))
Documentation:
Not documented as a variable.
[back]
Why am I not getting plain strings like I wanted?
\\([[:word:]]+\\)
.match-string-no-properties
.