Re: Question about pattern matching - Mailing list pgsql-general

From Craig Ringer
Subject Re: Question about pattern matching
Date
Msg-id 49511CD2.20208@postnewspapers.com.au
Whole thread Raw
In response to Question about pattern matching  (dimitris.sakellarios@telesuite.gr)
List pgsql-general
dimitris.sakellarios@telesuite.gr wrote:
> TABLENAME
>
> id    Candidate pattern
> -------------------------
> 1    0089258068520
> 2    008925806852
> 3    00892580685
> 4    0089258068
> 5    008925806
> 6    00892580
> 7    0089258
> 8    008925
> 9    00892
> 10    0089
> 11    008
> 12    00
> 13    0
>
>
> PATTERN
> -------
> 0089257000000
>
> QUESTION
> --------
> Pls let me know which is the best way to match the
> following pattern against this table and Receive id = 8
> that is the longest matching pattern from left to right.

The simplest way would be to select the id of the record with
max(length(pattern)), using a WHERE clause to filter for only matching
patterns by comparing the substring of both pattern and teststr of
length min(length(pattern),length(teststr)) for equality.

--
Craig Ringer

pgsql-general by date:

Previous
From: "Scott Marlowe"
Date:
Subject: Re: Question about pattern matching
Next
From: Julius Tuskenis
Date:
Subject: Re: Question about pattern matching