(setq str "1 '2015-08-14 7:11:00' GAR -0.29 89.10 -0.2795 0.375 8 0.6026 155.430000000 'GA Obler' 2015-08-14")
(string-match "\\b" str -1) ; gets the last match
因此,此示例应返回(1、23等)的列表。 我觉得我一定缺少一些全球匹配的功能? 或者,也许有必要使用一段时间循环并向前搜索/向后搜索。eDit
我最终写了此功能,但是我的ELISP很糟糕,所以问题仍然是这样做的,这是做到这一点的正确方法吗?或者是否有替代的内置函数已经可以了?
(defun match-positions (regexp str)
(let ((res '()) (pos 0))
(while (and (string-match regexp str pos)
(< pos (length str) ) )
(let ((m (match-end 0)))
(push m res)
(setq pos m)
) )
(nreverse res)
)
)
(match-positions "\'.*?\'\\|[-0-9.A-Za-z]+" str)
; (1 23 31 37 43 51 63 71 78 92 112 123)
使用
match-string
(require 'cl-lib)
(defun my/match-positions (regexp str)
"Find all positions where REGEXP matches in STR."
(save-match-data
(cl-loop for pos = 0 then (match-end 0)
while (string-match regexp str pos)
collect (match-end 0))))
or
(defun my/match-positions (regexp str)
"Find all positions where REGEXP matches in STR."
(save-match-data
(cl-loop with pos = 0
while (string-match regexp str pos)
collect (setf pos (match-end 0)))))