MapaDoSite ModificaçõesRecentes Notícias SeçãoElisp HowTo

RegexpOpt

regexp-opt is a package that helps you write very simple regexps.

 (regexp-opt '("alex" "albert" "alois" "bummer"))

results in

 "al\\(bert\\|ex\\|ois\\)\\|bummer"

(or

 "\\(?:al\\(?:bert\\|ex\\|ois\\)\\|bummer\\)"

in Emacs 23).

From the commentary:

The “opt” in “regexp-opt” stands for “optim\\(al\\|i\\(se\\|ze\\)\\)”.

This package generates a regexp from a given list of strings (which matches one of those strings) so that the regexp generated by:

 (regexp-opt strings)

is equivalent to, but more efficient than, the regexp generated by:

 (mapconcat 'regexp-quote strings "\\|")

The author, SimonMarshall?, also says:

Please don’t tell me that it doesn’t produce optimal regexps; I know that already. For example, the above explanation for the meaning of “opt” would be more efficient as “optim\\(al\\|i[sz]e\\)”, but this requires complex forward looking. But (ideas or) code to improve things (are) is welcome.
It appears to have improved since you wrote that; using Emacs 23 it gives me “\\(?:optim\\(?:al\\|i\\(?:[sz]e\\)\\)\\)” which also avoids unnecessary capturing.

CategoryRegexp