From: Tim Bradshaw on
On 2010-03-18 01:41:31 +0000, Rob Warnock said:

> [1] But, as we all know [thanks to JWZ & others],
> once you add regexps "you now have two problems"...

I think that what's not often appreciated is that *the second problem
is not regexps*. In fact, the second problem is that *the progrmmer
might now decide that regexps are the solution to all problems*.

From: RG on
In article <hnstgl$n6m$1(a)news.eternal-september.org>,
Tim Bradshaw <tfb(a)tfeb.org> wrote:

> On 2010-03-17 17:55:37 +0000, RG said:
>
> > So at the end of the day you'll be less lonely. But I'll smell
> > better.
>
> I prefer to think of it as "at the end of the day I'll be employed" :-)

That too.

rg
From: Rob Warnock on
RG <rNOSPAMon(a)flownet.com> wrote:
+---------------
| rpw3(a)rpw3.org (Rob Warnock) wrote:
| > MISMATCH, SEARCH, (FIND ... :TEST #'SEARCH), and POSITION-IF are
| > largely underappreciated for their usefulness in string hacking in CL.
|
| Interesting. I've never had occasion to use MISMATCH.
| What do you use it for?
+---------------

Mostly for checking whether a (possibly-abbreviated) fixed substring
exists at some specific but variable location in a string [especially
with the :START2/:END2 options], *without* having to do a SUBSEQ first
to extract the portion to be tested [avoiding unnecessary consing].
E.g., instead of this:

(equal "foo" (subseq string pos (+ pos 3))) ; or (length "foo") vice 3.

you can do this:

(not (mismatch "foo" string :start2 pos :end2 (+ pos 3)))

MISMATCH returns NIL only when the match is exact, and "a bounding
index of sequence-1" otherwise, but in many contexts the surrounding
control flow can be reversed to get rid of the NOT [e.g., swap WHEN
for UNLESS or v-v, etc.].

For case-independent matches, just add :TEST #'EQUALP.

It's also useful when parsing abbreviated keys in config files:

; Quick sketch, not tested:
(loop for abbrev-key across abbrev-keys
and index from 0
when (eql (mismatch abbrev-key line) (length abbrev-key))
return (values index (position #\space line)))

Or if you're trying to pull hex integers out of only lines that
start with some particular prefix:

(loop with prefix-length = (length prefix)
for line in lines
when (eql prefix-length (mismatch prefix line)) ; grep for the prefix
do (some-action (parse-integer line :start prefix-length
:radix 16 :junk-allowed t)))

SOME-ACTION is called here instead of just using COLLECT, in hopes
that the extracted numbers don't really need to be consed into a list.

Note that I used (EQL PREFIX-LENGTH (MISMATCH PREFIX LINE)) here
rather than (NOT (MISMATCH PREFIX LINE :END2 PREFIX-LENGTH)), since
the former requires that the LINE be at least one character longer
than PREFIX-LENGTH, something that *should* be the case if a hex
integer is to follow! ;-} The distinction between these two cases
is one of the subtleties of MISMATCH one has to keep in mind:

(MISMATCH "foo" "foo") ==> NIL
(MISMATCH "foo" "foo ") ==> 3


-Rob

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

From: RG on
In article <rNOSPAMon-E5D7CD.10064518032010(a)news.albasani.net>,
RG <rNOSPAMon(a)flownet.com> wrote:

> In article <hnstgl$n6m$1(a)news.eternal-september.org>,
> Tim Bradshaw <tfb(a)tfeb.org> wrote:
>
> > On 2010-03-17 17:55:37 +0000, RG said:
> >
> > > So at the end of the day you'll be less lonely. But I'll smell
> > > better.
> >
> > I prefer to think of it as "at the end of the day I'll be employed" :-)
>
> That too.
>
> rg

From the better-late-than-never department:

http://stereolambda.wordpress.com/2010/03/19/why-is-json-so-popular-devel
opers-want-out-of-the-syntax-business/

rg