ELisp: String Match in List

By Xah Lee. Date: . Last updated: .

Here's a function that check if a string X is a substring in any string of a list. Or vice versa.

(defun xah-string-match-in-list-p (Str ListOfString MatchCaseP &optional ReverseContainP)
  "If Str occur as substring in any element of list ListOfString, return true (the first element), else nil.

if ReverseContainP is true, change the direction of match. That is, true if any element in ListOfString occur in Str.

MatchCaseP 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.
2021-10-17 todo: use seq-some

URL `http://xahlee.info/emacs/emacs/elisp_string_match_in_list.html'
Version 2014-08-20 2021-11-29"
  (let ((case-fold-search (not MatchCaseP)))
    (if ReverseContainP
        (catch 'tag
          (mapc
           (lambda ($x)
             (when (string-match (regexp-quote $x) Str ) (throw 'tag $x)))
           ListOfString)
          nil)
      (catch 'tag
        (mapc
         (lambda ($x)
           (when (string-match (regexp-quote Str) $x ) (throw 'tag $x)))
         ListOfString)
        nil))))

Note: in Emacs 25 (Released 2016-09) , there are these new functions

see ELisp: Sequence Functions

Emacs Lisp, Filter List