The code from here accesses groups of a regular expression match in a buffer in interactive mode:
(let ((re (concat "\\([ \t]*" org-clock-string " *\\)"
"\\([[<][^]>]+[]>]\\)\\(-+\\)\\([[<][^]>]+[]>]\\)"
"\\(?:[ \t]*=>.*\\)?")))
(when (looking-at re)
(let ((indentation (match-string 1))
(start (match-string 2))
(to (match-string 3))
(end (match-string 4))
(use-start-as-default (equal end-as-default nil)))
But match-string n
fails with in non-interactive mode, for example in the *scratch*
buffer:
(string-match "\\([0-9]?[0-9]\\):\\([0-9]\\{2\\}\\)" "09:20")
0
(match-string 0)
#("save" 0 4 (fontified nil face font-lock-comment-face))
How can I access regular expression groups outside a buffer?
Update: after reading Strange behaviour of match-string/string-match adding the optional argument to match-string
doesn't help and I get an error:
(string-match "\\([0-9]?[0-9]\\):\\([0-9]\\{2\\}\\)" "09:20")
0
(match-string 0 "09:20") ;; Debugger entered--Lisp error: (args-out-of-range "09:20" 9 15)
When I run it again, then the last expression returns "09:20"
, even after I restart Emacs ([this thread from the comments]. When I run the code in emacs -q -nw
, then I get the error above.
(progn (string-match ...) (match-string ...))
?