From: Thorsten Jolitz <tjolitz@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: regexp question: match anything but not a group?
Date: Thu, 03 Apr 2014 12:50:43 +0200 [thread overview]
Message-ID: <87r45eit4s.fsf@gmail.com> (raw)
In-Reply-To: 533BC538.7010906@easy-emacs.de
Andreas Röhler <andreas.roehler@easy-emacs.de> 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 <andreas.roehler@easy-emacs.de> 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
next prev parent reply other threads:[~2014-04-03 10:50 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-01 21:30 regexp question: match anything but not a group? Thorsten Jolitz
2014-04-01 23:24 ` Pascal J. Bourguignon
2014-04-03 19:48 ` Thorsten Jolitz
[not found] ` <mailman.18864.1396554426.10748.help-gnu-emacs@gnu.org>
2014-04-03 20:24 ` Pascal J. Bourguignon
2014-04-03 21:36 ` Thorsten Jolitz
2014-04-02 5:55 ` Andreas Röhler
2014-04-03 19:36 ` Thorsten Jolitz
2014-04-03 22:19 ` Stefan Monnier
2014-04-04 8:39 ` Thorsten Jolitz
[not found] ` <mailman.18896.1396600696.10748.help-gnu-emacs@gnu.org>
2014-04-04 9:04 ` Loris Bennett
2014-04-04 9:37 ` Andreas Röhler
2014-04-04 10:40 ` Thorsten Jolitz
2014-04-04 14:11 ` Andreas Röhler
2014-04-04 6:37 ` Andreas Röhler
2014-04-04 8:53 ` Thorsten Jolitz
2014-04-02 7:21 ` Thorsten Jolitz
2014-04-02 8:07 ` Andreas Röhler
2014-04-03 10:50 ` Thorsten Jolitz [this message]
2014-04-03 23:46 ` Bob Proulx
2014-04-04 8:43 ` Thorsten Jolitz
2014-04-03 14:37 ` Stefan Monnier
2014-04-03 19:32 ` Thorsten Jolitz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87r45eit4s.fsf@gmail.com \
--to=tjolitz@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).