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 12:50:43 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Andreas Röhler <address@hidden> writes:

> Am 01.04.2014 23:30, schrieb Thorsten Jolitz:
>>
>> 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?
>>
>
>
> You need look-ahead-assertions, which aren't implemented in Emacs AFAIK.
> Here is a workaround to play with:
>
> (progn
>   (and (looking-at ".*")
>        (not (eq (char-after) ?\))))
>   (message "%s" (match-string-no-properties 0)))
>
> Test with more than one char ahead writing n char-after:
>
> (not (eq (char-after (+ 1 (point)) MY-CHAR)))
> (not (eq (char-after (+ 2 (point)) MY-CHAR)))

Since gmane.org was down sometime, this thread was continued with PMs,
so, just for reference, here is my solution to an extended form of the
original problem (i.e. determine if a given regexp is a regexp group or
not):

,-------------------------------------------------------
| Andreas Röhler <address@hidden> writes:
| 
| > Which would extend the question...
| > You wanted not to match any
| >
| > (regexp-quote "\\)")
| 
| I think I got it:
| 
| #+begin_src emacs-lisp
| (defun tj/regexp-group-p (rgxp)
|   "Return RGXP if its a regexp group, nil otherwise."
|   (with-temp-buffer
|     (insert (format "%S" rgxp))
|     (goto-char (point-min))
|     (if (ignore-errors
|           (save-excursion
|             (and
|              (re-search-forward "(")
|              (save-match-data
|                (looking-back
|                 (concat "^" (regexp-quote "\"\\\\("))
|                 (line-beginning-position)))
|              (goto-char (match-beginning 0))
|              (progn
|                (forward-sexp)
|                (looking-at
|                 "[*+]?[?]?\"$")))))
|         rgxp nil)))
| #+end_src
| 
| Thanks for you help!
`-------------------------------------------------------

-- 
cheers,
Thorsten




reply via email to

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