【问题标题】:Custom character classes in Emacs' regexpEmacs 正则表达式中的自定义字符类
【发布时间】:2014-06-27 14:29:27
【问题描述】:

是否可以在 emacs 中添加自己的字符类以便在正则表达式中使用它们?

假设,我想添加一个类[[:consonant:]],它匹配所有不是元音的字母,以避免一直写[b-df-hj-np-tv-z](是的,我知道我的快捷方式几乎与我想避免的术语,把它作为我的问题的简化)。

这可能吗,还是我必须分别使用formatconcat?如果可以的话,我该怎么做?

MWE 可能是这样的:

(defun myfun ()
  "Finds clusters of three or more consonants"
  (interactive)
  (if (search-forward-regexp "[b-df-hj-np-tv-z]\\{3,\\}")
    (message "Yepp, here is a consonant cluster.")
))

(defun myfun-1 ()
  "Should also find clusters of three or more consonants."
  (interactive)
  (if (search-forward-regexp "[[:consonant:]]\\{3,\\}")
    (message "Yepp, here is a consonant cluster.")
))

myfunmyfun-1 两个函数应该做同样的事情。

更进一步,我想知道是否可以将整个表达式放在这样的“快捷方式”中,例如

[[:ending:]] ==> "\\(?:en\\|st\\|t\\|e\\)"

【问题讨论】:

    标签: regex emacs elisp


    【解决方案1】:

    Jordon-Biondo 是正确的,你不能扩展 Emacs 的字符类来进行正则表达式搜索。如果您查看 Emacs 的源代码,您可以在 regex-emacs.c 的第 1510(ish) 行的例程 re_wctype_parse 中看到这些定义。因此,以本机方式添加这些将需要修改 .c 文件并重新构建。

    【讨论】:

      【解决方案2】:

      我不相信你能做到这一点。但是最近在ample-regexp package found HERE.中发布了类似的东西,这是从自述文件中摘录的:

      (define-arx h-w-rx
        '((h "Hello, ")
          (w "world"))) ;; -> hello-world-rx
      
      (h-w-rx h w) ;; -> "Hello, world"
      
      (h-w-rx (* h w)) ;; -> "\\(?:Hello, world\\)*"
      

      您可以使用它在一个大的define-arx 中定义各种别名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多