[分享] 在 mini-buffer 里输入 rx expression 而不是 regexp

贴上代码:

(defun mk/minibuffer-read-rx-expresion-to-regexp ()
  "Read rx expression and inert the converted regexp into the current minibuffer"
  (interactive)
  (insert (rx-to-string (read--expression "Enter expression: " "(seq )"))))
  
(define-key minibuffer-local-map (kbd "C-r") #'mk/minibuffer-read-rx-expresion-to-regexp)

rx expression 比 regexp 更加直观,因此我想在 mini-buffer 也输入 rx expression 例子:

(rx bol "El" (or "Psy" "Kongaroo") eol)
=> "^El\\(?:Kongaroo\\|Psy\\)$"

关于 rx 的更多内容请阅读: (info “(elisp) Rx Notation”)

2 个赞