Subject: Re: find substring
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 13 Feb 2004 08:06:16 -0600
Newsgroups: comp.lang.lisp
Message-ID: <p7GcnX-NMslFRLHdXTWc-w@speakeasy.net>
Coby Beck <cbeck@mercury.bc.ca> wrote:
+---------------
| "Albert Reiner" <albert@chello.at> wrote in message
| > I cannot seem to find in the HyperSpec or CLtL2 a way of checking whether
| > some string is a substring of another one.
| 
| (search "waldo" "where the heck's waldo?")  =>  17
+---------------

And in cases where an anchored match is more appropriate:

    (mismatch "there" "here & there & everywhere" :start2 7)  =>  5

or for partial matches (also left-anchored):

    (mismatch "their" "here & there & everywhere" :start2 7)  =>  3

which says that only the first 3 chars matched.

SEARCH provides the {start,end}{1,2} keywords, too, which can be
helpful in avoiding intermediate garbage when doing incremental
searches, e.g.:

    (loop with target = "where's waldo? what's waldo? when's waldo?"
	  and start = 0
	  for pos = (search "waldo" target :start2 start)
      while pos
	collect pos
	do (setf start (1+ pos)))
    => (8 22 36)


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607