我已经在Emacs Lisp文档中无处不寻找如何在字符串中进行正则表达式搜索的方法。我找到的都是如何在缓冲区中进行这样的操作。
我是否遗漏了什么?我应该将我的字符串分割成临时缓冲区,然后在那里进行搜索吗?这是elisp的编码风格吗?我会逐渐习惯吗?这个问题是否有标准解决方案?当我应该能够直接搜索已经存在的变量时,操作缓冲区似乎很繁琐。
这里是Emacs维基上关于字符串内容和缓冲区内容的讨论。只需将您的字符串存储为变量。
关于字符串的棘手之处在于通常情况下不修改字符串本身(除非对字符串执行数组函数,因为字符串是一个数组,但这应该尽量避免),而是返回修改后的字符串。
无论如何,这里是使用elisp中字符串的示例。
这将从字符串末尾删除空格:
(setq test-str "abcdefg ")
(when (string-match "[ \t]*$" test-str)
(message (concat "[" (replace-match "" nil nil test-str) "]")))
string-match
。如果您需要重复匹配,请使用其返回的索引作为下一次调用的可选“start”参数。文档在ELisp手册中,章节“正则表达式搜索”中。replace-regexp-in-string
。(defun string-starts-with-p (string prefix)
"Return t if STRING starts with PREFIX."
(and
(string-match (rx-to-string `(: bos ,prefix) t)
string)
t))
(defun string-ends-with-p (string suffix)
"Return t if STRING ends with SUFFIX."
(and (string-match (rx-to-string `(: ,suffix eos) t)
string)
t))