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: Thu, 03 Apr 2014 22:24:42 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Thorsten Jolitz <tjolitz@gmail.com> writes:

> "Pascal J. Bourguignon" <pjb@informatimago.com> writes:
>
>> Thorsten Jolitz <tjolitz@gmail.com> 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 "\\)")
>> --> "\\(?:\\`[^)\\]*\\(?:[^\\]\\|\\\\[^)]\\)[^)\\]*\\'\\)"
>
> Wow, impressive, thank you. 
>
> At first sight reads like pseudo-code to me, probably more CL-style than
> elisp style. 
>
> any, anything, otherwise ... unusual stuff, I don't even find those
> functions with C-h f (not even after loading cl.el and cl-extra.el). 
>
> This is definitely hard to digest ...

rx is a famous emacs lisp library. (require 'rx)
case is in (require 'cl) which should be in everybody's ~/.emacs
The rest is DATA!    


all wasn't used, it's for a little optimization:

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

-- 
__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]