Given a regexp A, how do I derive the inverse regexp B such that given any text exactly one of the two regexes will match it.

For example in the below example:

(rx (or (group ---regexpA---) ; a match
        (group ---regexpB---))) ; every group of text that's not matched by above

It should be the case that the concatenation of the list of matches should be exactly the contents of the buffer.

(string= (buffer-string)
         (let (string)
           (while (re-search-forward regexp nil t)
             (setq string (concat string
                                  (or (match-string-no-properties 1)
                                      (match-string-no-properties 2)))))

Sounds like this is maybe an X Y question. What is it that you're really trying to do?

Except for some simple regexps (e.g. character classes, [...], where [^...] means not), a regexp cannot find a non-match. A regexp is not negative.

The way to get the effect of negative matching is to use Elisp to first match and then take the complement, that is, ignore or exclude whatever matches, using Lisp code to do what you want with the text that didn't match a regexp.

| improve this answer | |
  • Ok I'll consider posting what I am trying to do in a separate question. – Aquaactress Mar 5 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.