I've started playing around with elisp to create font-locks (syntax hig开发者_开发技巧hlighting) in Emacs. Most of the tutorials out there mention using an elisp function "regexp-opt" to optimize regular expressions, but my version of emacs (23.2) doesn't seem to have that function. Furthermore, Googling for the answer doesn't seem to turn up useful results. Any ideas?
As you can find out via C-h f regexp-opt
:
regexp-opt is a compiled Lisp function.
(regexp-opt strings &optional paren)
Return a regexp to match a string in the list strings. Each string should be unique in strings and should not contain any regexps, quoted or not. If optional paren is non-nil, ensure that the returned regexp is enclosed by at least one regexp grouping construct. The returned regexp is typically more efficient than the equivalent regexp:
(let ((open (if paren "\(" "")) (close (if paren "\)" "")))
(concat open (mapconcat 'regexp-quote strings "\|") close))If paren is `words', then the resulting regexp is additionally surrounded by \< and >.
Note, that it is a function to be used in Lisp code, not an interactive command which you could run with M-x
regexexp-opt is a elisp function but not an emacs command. That is why you cannot execute it by running: M-x regexp-opt
However, you can execute any elisp function from the elisp shell. Type in M-x eshell. And from this shell you can run regexp-opt
As others have said, regexp-opt
is not a command (so you cannot invoke it using M-x
). But you can invoke an application of it interactively, using M-:
.
For example, M-: (regexp-opt '("foo" "toto" "blat" "total" "mistral")) RET
returns the regexp "\\(?:blat\\|foo\\|mistral\\|tot\\(?:al\\|o\\)\\)"
.
精彩评论