获取字符串中regexp匹配项的所有位置
如何使用elisp在字符串中存储所有regexp匹配的位置?下面是一个示例,我想获得字符串中单词/数字的所有结尾的位置,或者如果是单引号,则是单引号短语的结尾获取字符串中regexp匹配项的所有位置,regex,emacs,elisp,Regex,Emacs,Elisp,如何使用elisp在字符串中存储所有regexp匹配的位置?下面是一个示例,我想获得字符串中单词/数字的所有结尾的位置,或者如果是单引号,则是单引号短语的结尾 (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) ; g
(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等)的列表。我觉得我一定缺少了一些全局匹配的函数?或者,可能需要使用while循环并向前/向后搜索
编辑
我最终编写了这个函数,但我的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)
(取消匹配位置(regexp str)
(让((res’())(pos 0))
(while(and)(字符串匹配regexp str pos)
(
使用:
使用:
这只找到一个匹配项吗?我真的不明白这是怎么匹配的这只找到一个匹配吗?我真的不明白这是怎么给比赛的
(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")
(save-match-data
(and (string-match "\\b" str)
(let ((first_match (match-string 0 str))
(second_match (match-string 1 str))
)
;; your code
)))