help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regexp question: match anything but not a group?


From: Pascal J. Bourguignon
Subject: Re: regexp question: match anything but not a group?
Date: Wed, 02 Apr 2014 01:24:52 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Thorsten Jolitz <address@hidden> writes:

> Hi List, 
>
> how can I write a regexp that acts like e.g.
>
> ,------
> | ".*?"
> `------
>
> but does not match a group like e.g. 
>
> ,---------------------
> | (regexp-quote "\\)")
> `---------------------
>
> ?
>
> This works more or less but does not seem to be very robust
>
> ,---------
> | "[^)]*?"
> `---------
>
> since ')' could appear in other contexts than the group. How can I
> negate a specific group of characters and not only any occurence of
> single characters?

(defun pjb-rx-not-string (string)
  (case  (length string)
    ((0)        `(* anything))
    ((1)        `(not (any ,string)))
    (otherwise  `(or (not (any ,(subseq string 0 1)))
                     (seq ,(subseq string 0 1)
                          ,(pjb-rx-not-string (subseq string 1)))))))


(defun pjb-regexp-not-string (string)
  (let ((all (coerce (delete-duplicates
                      (sort (coerce string 'list) (function <))) 'string)))
    (rx-to-string `(seq bot
                        (* (not (any ,string)))
                        ,(pjb-rx-not-string string)
                        (* (not (any ,string)))
                        eot))))

(pjb-regexp-not-string "\\)")
--> "\\(?:\\`[^)\\]*\\(?:[^\\]\\|\\\\[^)]\\)[^)\\]*\\'\\)"


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ?  C'est le moment d'acheter !"




reply via email to

[Prev in Thread] Current Thread [Next in Thread]