Elisp: String Match in List

By Xah Lee. Date: .

Here's a function that check if a string occur in a list.

(defun xah-string-match-in-list-p (@str @list-of-string @match-case-p &optional @reverse-contain-p)
  "If @str occur in list @list-of-string, return true (the first element), else nil.

if @reverse-contain-p is true, change the direction of match. That is, true if any element in @list-of-string occur in @str.

@match-case-p determines whether case is literal for the match.

No regex is used.

Existing match data is changed. Wrap it with `save-match-data' if you need it restored.

URL `http://ergoemacs.org/emacs/elisp_string_match_in_list.html'
Version 2016-07-18"
  (let ((case-fold-search (not @match-case-p)))
    (if @reverse-contain-p
        (catch 'tag
           (lambda ($x)
             (when (string-match (regexp-quote $x) @str ) (throw 'tag $x)))
      (catch 'tag
         (lambda ($x)
           (when (string-match (regexp-quote @str) $x ) (throw 'tag $x)))

see also Elisp: Filter a List

Note: in emacs 25.1 (released on ), there are these new functions

They are in seq.el. You need (require 'seq).

Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.