7

使用xr帮助分析Elisp正则表达式

 3 years ago
source link: https://www.lujun9972.win/blog/2020/11/23/%E4%BD%BF%E7%94%A8xr%E5%B8%AE%E5%8A%A9%E5%88%86%E6%9E%90elisp%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用xr帮助分析Elisp正则表达式

Elisp 的正则表达式语法以繁杂著称,分析起来特别麻烦。今天发现一个 xr 库非常好用,可以把Elisp正则表达式语法翻译成 rx 的格式,方便我们对其进行分析。 例如我们尝试使用 xrorg-heading-regexp 进行分析:

(xr org-heading-regexp)
(seq bol
     (group
      (one-or-more "*"))
     (opt
      (one-or-more " ")
      (group
       (*? nonl)))
     (zero-or-more
      (any "     "))
     eol)

从中可以很清楚的看到整个正则表达式分成5个部分:

  1. bol 表示行首
  2. (group (one-or-more "*")) 一个或多个星号组成的分组
  3. (opt (one-or-more " ") (group (*? nonl))) 一个可选项,由一个或多个空格和任意多个非换行字符的分组(其中 *? 说明使用贪婪算法)组成。
  4. (zero-or-mode (any " ")) 任意多个制表符或空格
  5. eol 表示行末

注意到,结果中有很多类似 bol, nonl, eol 这样的缩写,我们可以通过将第二个参数设置为 'verbose 来输出详细说明:

(xr org-heading-regexp 'verbose)
(seq line-start
     (group
      (one-or-more "*"))
     (zero-or-one
      (one-or-more " ")
      (group
       (*? not-newline)))
     (zero-or-more
      (any "     "))
     line-end)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK