[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RegExp: match everything except a certain string
From: |
Stefan Monnier |
Subject: |
Re: RegExp: match everything except a certain string |
Date: |
Fri, 30 Nov 2007 11:26:32 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.50 (gnu/linux) |
> [^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
"\\|")
"\\)")))))
Message not available
- Re: RegExp: match everything except a certain string,
Stefan Monnier <=