Given a list:
(setq mylist '("a1" "a2" "b1" "b2"))
How to delete all elements that match ^a
?
What I want is "a1"
and "a2"
deleted, only left "b1"
and "b2"
in the list.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityDo you want a new list that has only elements "b1"
and "b2"
? Or do you want the same list structure, but modified to have removed elements "a1"
and "a2"
?
@rpluim shows one way to do the former. A similar way to do the latter is to use cl-delete-if
instead of cl-remove-if
.
(setq mylist (cl-delete-if (lambda (k) (string-match-p "^a" k)) mylist)
(Make sure you set variable mylist
to the result returned by cl-delete-if
, if you expect that variable's value to reflect the deletion properly in all cases. See the Elisp manual, node Rearrangement for more about this.)
i
in it, but single-character edits are not allowed. sigh
There are many ways to do this, here's one:
(setq mylist (cl-remove-if (lambda (k)
(string-match "^a" k))
mylist)
^
matches beginning-of-line, not beginning-of-string; see(elisp) Syntax of Regexps
for how to do the latter, which is probably what you want here.alist
(because there was none -- I honestly thought it was probably a contraction of "a list" which was written without knowledge of association lists). You've been adding them, though :) AFAICS the question isn't actually about alists.