How to create shy groups in Emacs using rx?

Typically, I can use the excellent rx macro to create readable regular expressions and make sure that I escape the correct metacharacters.

 (rx (any "AZ")) ;; "[AZ]" 

However, I cannot figure out how to create shy groups, for example. \(?:AB\) . rx sometimes produces them in its output:

 (rx (or "ab" "bc")) ;; "\\(?:ab\\|bc\\)" 

but I want to explicitly add them. I can do:

 (rx (regexp "\\(?:AB\\)")) 

but it defeats the rx point.

In an ideal world, I would like to write:

 (rx (shy-group "A")) 

I would agree to something like this (none of these works):

 ;; sadly, `regexp` only accepts literal strings (rx (regexp (format "\\(?:%s\\)" (rx WHATEVER)))) ;; also unfortunate, `eval` quotes the string it given (rx (eval (format "\\(?:%s\\)" (rx WHATEVER)))) 

How to create regular expressions with shy groups using rx ?

+4
source share
2 answers

I think the structure of the rx form eliminates the need to explicitly create shy groups - everything that a shy group might need is due to a different syntax.

eg. your own example:

 (rx (or "ab" "bc")) ;; "\\(?:ab\\|bc\\)" 
+4
source

In other cases, it is also possible to extend the keywords used by rx .

Example (taken from the EmacsWiki link above):

 (defmacro rx-extra (&rest body-forms) (let ((add-ins (list `(file . ,(rx (+ (or alnum digit "." "/" "-" "_")))) `(ws0 . ,(rx (0+ (any " " "\t")))) `(ws+ . ,(rx (+ (any " " "\t")))) `(int . ,(rx (+ digit)))))) `(let ((rx-constituents (append ',add-ins rx-constituents nil))) ,@body-forms))) 
+1
source

Source: https://habr.com/ru/post/1485969/


All Articles