From: Stefan Monnier <monnier@iro.umontreal.ca>
To: help-gnu-emacs@gnu.org
Subject: Re: RegExp: match everything except a certain string
Date: Fri, 30 Nov 2007 11:26:32 -0500 [thread overview]
Message-ID: <jwvprxr1z96.fsf-monnier+gnu.emacs.help@gnu.org> (raw)
In-Reply-To: mailman.3839.1195580948.18990.help-gnu-emacs@gnu.org
> [^s]?[^u]?[^b]?[^t]?[^i]?[^t]?[^l]?[^e] = {},?$
Nice try, but no cigar ;-)
How 'bout something like:
"\\(?:[^s]\\|s\\(?:[^u]\\|u\\(?:[^b]\\|b\\(?:[^t]\\|t\\(?:[^i]\\|i\\(?:[^t]\\|t\\(?:[^l]\\|l\\(?:[^e]\\|e[^\t\n ]\\)\\)\\)\\)\\)\\)\\)\\)"
this is just a starting point, because even if you have "subtitle" in
your buffer the "title" part doesn't match "subtitle", so
a regexp-search may find "title" as a match: negation in regexp search
is pretty tricky.
Stefan
(defun regexp-not-in (words endchars &optional prefix)
"Return a regexp that matches anything other than words in WORDS.
ENDCHARS is a list containing the chars that can appear after a word."
;; `prefix' is only used internally.
(if prefix (setq words (all-completions prefix words)) (setq prefix ""))
(let* ((pos (length prefix))
(empty (when (member prefix words)
(setq words (remove prefix words)) t))
(chars (delete-dups (mapcar (lambda (word) (aref word pos)) words)))
(chars-re (regexp-opt-charset (if empty (nconc chars endchars) chars)))
(nonchars-re (concat "[^" (substring chars-re 1 -1) "]")))
(cond
((and empty (null endchars))
;; If there are no termination chars, then we have to simply disallow
;; this match altogether.
"\\`")
((null chars) nonchars-re)
(t
(concat "\\(?:" nonchars-re
"\\|"
(mapconcat (lambda (char)
(setq char (string char))
(concat char
(regexp-not-in words endchars
(concat prefix char))))
chars
"\\|")
"\\)")))))
prev parent reply other threads:[~2007-11-30 16:26 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-20 16:08 RegExp: match everything except a certain string Sven Bretfeld
2007-11-20 17:50 ` Andreas Röhler
2007-11-20 20:36 ` Sven Bretfeld
2007-11-20 21:05 ` Lennart Borgman (gmail)
2007-11-20 21:31 ` Sven Bretfeld
[not found] ` <mailman.3846.1195590989.18990.help-gnu-emacs@gnu.org>
2007-11-28 16:19 ` Sven Utcke
2007-11-28 21:22 ` Malte Spiess
[not found] ` <mailman.3839.1195580948.18990.help-gnu-emacs@gnu.org>
2007-11-30 16:26 ` Stefan Monnier [this message]
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=jwvprxr1z96.fsf-monnier+gnu.emacs.help@gnu.org \
--to=monnier@iro.umontreal.ca \
--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).