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: Thorsten Jolitz
Subject: Re: regexp question: match anything but not a group?
Date: Thu, 03 Apr 2014 23:36:55 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

> 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)

I've seen that one ...
Ok, being aware of rx.el the whole thing appears less crytic. 

> case is in (require 'cl) which should be in everybody's ~/.emacs

I know 'case from PicoLisp and really like it. If only the cl 'case
would compare with equal too and not only with eql...

> The rest is DATA!    

yes, I know, but still ... not the usual elisp you see everyday. 

> 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))))

I begin to understand ...

-- 
cheers,
Thorsten




reply via email to

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