我尝试匹配一个字符串,然后使用grouping创建一个新的字符串:
(let ((url (browse-url-url-at-point)))
(if (string-match "http://domain/\\([0-9]+\\)/\\([a-z]+\\)\.\\([0-9]+\\)" url)
(setq filename (concat (match-string 1 url) "_" (match-string 2) "." (match-string 3) ".xml"))))当我(打印url)时,我得到以下信息
"http://domain/1234/action.1234567"在成功匹配后,当I(打印文件名)时,我得到以下内容:
#("1234_ublish.eport s.xml" 0 5 nil 5 11 (face nxml-element-local-name-face fontified t) 11 12 nil 12 17 (face nxml-element-local-name-face fontified t) 17 18 (fontified t) 18 19 (face nxml-attribute-local-name-face fontified t) 19 23 nil)为什么会发生这种情况?
发布于 2009-06-08 14:57:01
您的第二个和第三个匹配字符串没有包含可选的字符串参数。根据匹配字符串文档,“如果最后一次搜索是通过字符串上的‘string -match’进行的,则应提供字符串。”
发布于 2009-06-08 14:59:30
我找到问题了。
原来( string -match)需要将原始字符串作为参数,否则它将返回包含奇怪值的奇怪列表(不确定这些值是什么)。
无论如何,将关于代码更改为:
(let ((url (browse-url-url-at-point)))
(if (string-match "http://domain/\\([0-9]+\\)/\\([a-z]+\\)\.\\([0-9]+\\)" url)
(setq filename (concat (match-string 1 url) "_" (match-string 2 url) "." (match-string 3 url) ".xml"))))修复问题
发布于 2009-06-08 19:07:10
作为mamboking already mentioned,match-string的文档字符串告诉您有关以下内容的所有信息:
(match-string NUM &optional STRING)
⋮
STRING should be given if the last search was by `string-match' on STRING.如果你还查看了string-match的文档,你会发现它建议使用match-beginning和match-end来获取匹配,这些都是C中的内置函数。
(if (string-match "\\([a-z]\\)" "123 test string")
(match-beginning 1)) ;; 4这些函数只返回匹配文本的开始或结束位置,这就是为什么match-string也需要原始字符串的原因。当使用search-forward或re-search-forward时,match-beginning和match-end将返回缓冲区位置,因此match-string可以很容易地从缓冲区的内容中子串有趣的匹配。
您可能还想看看match-string-no-properties,它的行为与match-string相同,期望它返回没有文本属性的匹配文本字符串。
https://stackoverflow.com/questions/965193
复制相似问题