[Pdx-pm] Re: string function

paull at peak.org paull at peak.org
Thu Oct 30 00:55:15 CST 2003


Ward writes:
> I'm having trouble thinking of a good way to compare two strings and
> find out where they differ.
>
> For example, given:
>
>         "abcdewxyz"
>         "abcvwxyz"
>
> I would like to find the number of matching characters starting from
> the front or back of the strings. These would be,
>
>         3 match from the front
>         4 match from the back

A-Good-Way is in the eyes of the beholder.

Converting 'abcdewxyz' into

    '^((?:a(?:b(?:c(?:d(?:e(?:w(?:x(?:y(?:z)?)?)?)?)?)?)?)?)?)'
and
    '((?:(?:(?:(?:(?:(?:(?:(?:(?:a)?b)?c)?d)?e)?w)?x)?y)?z)?)$'

will yield matches of the appropriate length.  If you can amortize the
cost of compiling a rex across many matches then perhaps this would even
be a performer.  It certainly has readability going for it.

-pal




More information about the Pdx-pm-list mailing list