0

How can I get all elements of a list which don't match multiple patterns? The patterns are given as a list.

For instance, I want all files without '("no" "nix") in them.

(setq se-have
      '("~/site/src/good-org.org"
        "~/site/src/bad-no-org.org"
        "~/site/src/yes-to-org.org"
        "~/site/src/nix-this.org"
        "~/site/src/know-this-will-go.org"
        "~/site/src/is-always-next-to.org"))
(setq se-want
      '("~/site/src/good-org.org"
        "~/site/src/yes-to-org.org"
        "~/site/src/is-always-next-to.org"))

I can do it for a single pattern:

(seq-filter
 (lambda (x) (not (string-match-p (regexp-quote "no") x)))
 se-have)

How could I do this for each pattern in a list?

  • 2
    Note that seq-filter together with a negated not predicate is the same as seq-remove with the not removed. – Basil Jul 21 at 8:22
3

You're essentially saying you want to match any of a list of patterns.

regexp-opt takes a list of strings and produces a single regexp which matches any of them.

(regexp-opt '("no" "nix"))
=> "\\(?:n\\(?:ix\\|o\\)\\)"

Note that the individual strings in the input list are not regexps -- the end result will be analogous to using regexp-quote for each string.

The equivalent rx syntax is:

(rx (or "no" "nix"))
| improve this answer | |
  • Accepted because it's easy to generalize and works well enough. Unfortunately, it returns "" when passed nil which results in nil overall instead of all the files being returned (i.e. no files excluded). But my users will never use it that way, right? ;) – Lorem Ipsum Jul 21 at 18:17
  • 1
    if EXCLUSIONS then FILTER LIST else LIST – phils Jul 21 at 22:18
  • Of course! Thank you for the help, again. – Lorem Ipsum Jul 22 at 1:45
3
(seq-filter
 (lambda (x) (and (not (string-match-p (regexp-quote "no") x))
                  (not (string-match-p (regexp-quote "nix") x))))
  se-have)

Or use cl-remove-if-not. Or use seq-filter or cl-remove-if-not twice, instead of and. And so on.

| improve this answer | |

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.