4

Is it possible to add own character classes to emacs in order to use them in regular expressions?

Let's say, i want to add a class [[:consonant:]] which matches all letters that are not vowels in order to avoid writing [b-df-hj-np-tv-z] all the time (and yes, i am aware that my shortcut is almost as long as the term i want to avoid, take it as a simplification of my problem).

Is this possible at all or do i have to use format or concat, respectively? If it is possible, how do i do that?

An MWE could be like this:

(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.")
))

Both functions myfun and myfun-1 should do the very same thing.

One step further i'd like to know if it is possible to put whole expressions in such "shortcuts", like

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

2 Answers 2

3

Jordon-Biondo is correct, you cannot extend Emacs' character classes for regexp search. If you peek into Emacs' source code, you can see these defined in the routine re_wctype_parse on line 1510(ish) of regex-emacs.c. So adding to these natively would require modifying the .c file and rebuilding.

Sign up to request clarification or add additional context in comments.

Comments

1

I do not believe you can do this. But there is something similar that was recently released in the ample-regexp package found HERE. This was taken from the readme as an example:

(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\\)*"

You could use this to define a wide range of aliases in one big define-arx.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.