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 list ListOfString, return that element (true), 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.

`match-data' is changed.

2021-10-17 todo: use seq-some

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