EmacsTalk

通过函数调用进行字符串替换

  tags: regexp text

在 Emacs 中,有如下几种文本替换的函数:

静态替换是最常见的,比如将 foobar 替换成 FOObar ,但是有的时候需要对匹配的文本进行一些处理, 这时就要用到函数调用了。 replace-regexp 就支持函数调用。

1M-x replace-regexp RET regexp RET newstring RET

如果输入的 newstring 是以 \, 开头,那么后面就可以跟一个表达式,为了用上匹配的文本,有一些特殊字符:

上面这两个特殊字符返回的类型是 string,如果匹配的文本是数字,那么可以用 \#& \#d 表示。比如有如下文本:

123
456
789

执行 M-x replace-regexp RET [0-9]\{3\} RET \,(+ \#& \#&) 后,会得到:

246
912
1578

在写 Lisp 代码时, replace-regexp 对应的是

1(while (re-search-forward REGEXP nil t)
2  (replace-match TO-STRING nil nil))

需要注意一点的时,在写 Lisp 代码时,转义字符需要有两个反斜线,比如 [0-9]\\{3,\\} ,这时因为:

反斜线不仅在 regexp 中是特殊字符,在 lisp 这门语言中,也是特殊字符。

比如,在 Lisp 中需要用 "\\" 表示一个反斜线字面量,如果直接写 [0-9]\{3,\} 那么在经过 lisp 的 parser 后, 得到的是 [0-9]{3} ,而这个正则并不是说匹配一个数字三次,而只是一个数字,后面跟着 {3} 这三个字符而已。

这是在 Lisp 中写正则比较容易出错的一点,Manual 中的相关资料:

最后,比如有如下文本,需要把其中的 unix 时间戳替换为可读的时间,lisp 代码怎么写?

 1[
 2  {
 3    "value": 0.000521920668058455,
 4    "timestamp": 1718598630000
 5  },
 6  {
 7    "value": 0.0002609603340292275,
 8    "timestamp": 1718598645000
 9  },
10  {
11    "timestamp": 1718598675000
12  },
13  {
14    "timestamp": 1718598705000
15  },
16  {
17    "timestamp": 1718598720000
18  }
19]

答案:

1(defun ts->human-date ()
2  (interactive)
3  (save-excursion
4    (goto-char (point-min))
5    (while (re-search-forward "\\b[0-9]\\{13\\}\\b" nil t)
6      (replace-match (thread-last (/ (string-to-number (match-string 0))
7                                     1000)
8                                  (seconds-to-time)
9                                  (format-time-string "%Y-%m-%d %H:%M:%S %z"))))))

最终结果:

 1[
 2  {
 3    "value": 0.000521920668058455,
 4    "timestamp": 2024-06-17 12:30:30 +0800
 5  },
 6  {
 7    "value": 0.0002609603340292275,
 8    "timestamp": 2024-06-17 12:30:45 +0800
 9  },
10  {
11    "timestamp": 2024-06-17 12:31:15 +0800
12  },
13  {
14    "timestamp": 2024-06-17 12:31:45 +0800
15  },
16  {
17    "timestamp": 2024-06-17 12:32:00 +0800
18  }
19]

收听方式

反馈